Spring 3.1.2 adds support for automatic conversion to JSON via Jackson 2.x. There isn’t much that you need to do to make it work, with on caveat.
You need to be using at least Spring 3.1.2 to have Jackson 2.x support. Add the Jackson 2.x dependencies.
1
2
3
4
5
6
7
8
9
10
|
< dependency >
< groupId >com.fasterxml.jackson.core</ groupId >
< artifactId >jackson-core</ artifactId >
< version >2.0.4</ version >
</ dependency >
< dependency >
< groupId >com.fasterxml.jackson.core</ groupId >
< artifactId >jackson-databind</ artifactId >
< version >2.0.4</ version >
</ dependency >
|
Game项目中pom
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.8.5</version>
</dependency>
因为使用maven管理jar包,会自动下载依赖的jackson-core-asl-1.8.5.jar
NOTE: If you only include the jackson-core artifact and do not include the jackson-databind artifact, your spring requests will return HTTP 406 errors and there will be no indication of what the real problem is.
What happens if you don’t include jackson-databind? The <mvc:annotation-driven/> tag will not instantiate a MappingJackson2JsonView because it has an import for com.fasterxml.jackson.databind.ObjectMapper. There is no message because it just thinks that you are not using that.
Your spring mvc context should already have the <mvc:annotation-driven/>.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<? xml version = "1.0" encoding = "UTF-8" ?>
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
< context:component-scan base-package = "com.appriss.jxp" />
< mvc:annotation-driven />
<!-- Resolves view names for views returned by controllers -->
< bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass = "org.springframework.web.servlet.view.JstlView"
p:prefix = "/WEB-INF/jsp/"
p:suffix = ".jsp" />
</ beans >
|
Create a controller method. There are 2 things that must be done. First, specify that the method does produce json as the output. That will map against the “accept-header: application/json”. Then have the method return an object instead of a ModelAndView object. Annotate that object with @ResponseBody.
1
2
3
4
5
6
7
8
9
|
/** * Returns a json list of first names.
* @param term the beginning part of the first name
* @return json string array of first names
*/
@RequestMapping (value = "/first" , produces = "application/json" )//produces spring-web3.1以上才支持
public @ResponseBody List<String> firstNames( @RequestParam String term) {
return nameDao.getFirstNames(term);
} |
There you have it. The rest is automatic.
相关推荐
spring mvc 常用开发框架 jar包 提高开发效率 降低开发难度
Ajax With Spring 3. Eclipse 工程,包含Web所需要的 所有jar包。 1> ajax 请求。 2> spring 3注解使用 3> mvc:annotation
总有4个包 jackson-core-asl-1.9.13.jar jackson-core-lgpl-1.9.13 jackson-mapper-asl-1.9.13 jackson-mapper-lgpl-1.9.13
spring4 mvc 配置json 所需要的依赖jar包很全
spring mvc 返回json 接口 完整demo
讲述了spring2.0,2.5以及portlet的mvc的搭建以及怎样使用
这是一个使用Spring2.0的MVC书写一个web应用。是学习spring2.0开发web应用的好例子。
使spring-mvc支持ajax 包含源代码以及使用教程
spring3 MVC实战
主要利用hibernate3.0+struts2.0 +dwr1+spring框架
Spring web MVC和spring 2.0 form tag的解说,里面的内容可以当作平时的工具书查看
spring2.0写的一个登录程序,完整下下来就可以用哈,本来想在集成hibernate和jpa,但现在还在研究哈,谁会,也邮件给我哈 mende_001@163.com
最近在整spring mvc 返回json的配置,网上搜集整理了一下,觉得蛮有用的就共享出来,三个必备json包并附spring mvc 返回json的配置,此包有两个版本json包,一个2.6一个2.2
spring MVC 传json的简单实现 很入门 也很实用
Spring MVC返回JSON数据 用到的jar包,主要是jackson-all-1.9.0.jar,jackson-all-1.9.9.jar,jackson-all-1.9.11.jar三个包
《Pro Spring MVC With Web Flow 》是《 精通SpringMVC 》的全英文版,对java开发人员来说,读最原始的原汁原味的技术文档,才不会被有些翻译不合理的书籍所误导其本意,对自己本身来说也是一种提高。
Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了。Spring3 MVC结构简单,应了那句话简单就是美,而且他强大不失灵活,性能也很优秀。 Spring3 MVC的优点: 1、...
Pro Spring MVC_With Web Flow-Apress(2012) Pro Spring MVC_With Web Flow-Apress(2012)
林信良著 spring2.0技术手册 《Spring 2.0技术手册》介绍了Spring 2.0的新特性,诸如Spring 2.0的新配置、新AOP支持、增强的IoC、JDBC和form标签等新功能。它通过实际完成一个完整的Spring项目示例,展示了与...