1、引入jackson包
要想在springmvc框架下支持json的转换,需要引入jackson的包,在pom.xml中添加如下代码:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.7</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.7</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.7</version> </dependency>
没有用maven的可以去官网下载jar包
修改springMVC-servlet.xml文件,修改mvc:annotation-driven标签对应的内容,其实引用了jackson的包,默认就加了对json的转换了,我试了一下,不添加也可以用。
<mvc:annotation-driven> <mvc:message-converters> <ref bean="stringHttpMessageConverter"/> <ref bean="mappingJackson2HttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean>
2、pojo类的转换
添加一个pojo类 User.java
public class User { private String username; private String fav; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getFav() { return fav; } public void setFav(String fav) { this.fav = fav; } }
Controller类中将User对象转换成json字符串输出:
@RequestMapping("/test1") @ResponseBody public User test1() { User user = new User(); user.setUsername("张三"); user.setFav("乒乓球"); return user; }
也可以这么写:
@RequestMapping("/test1") public @ResponseBody User test1(HttpServletRequest request) { User user = new User(); user.setUsername("张三"); user.setFav("乒乓球"); return user; }
输出的结果是:
{"username":"张三","fav":"乒乓球"}
3、Map的转换
如果没有pojo类,只是一些键值对的输出,可以使用Map
@RequestMapping("/test2") @ResponseBody public Map<String,String> test2() { Map<String,String> map = new HashMap<String,String>(); map.put("username", "张三"); map.put("fav", "乒乓球"); return map; }
输出的结果跟上面的一样:
{"username":"张三","fav":"乒乓球"}
4、接收json串做为参数
@RequestMapping(value="/test3",consumes="application/json",method=RequestMethod.POST) public String test3(@RequestBody User user,HttpServletRequest request) { request.setAttribute("username", user.getUsername()); request.setAttribute("fav", user.getFav()); return "/test3"; }
这里是使用@RequestBody User user作为接收参数,如果要接收json,接收方式只能是post方式,不然会报错
这里我是返回到了一个jsp页面,这里就不把jsp页面的内容写出来了
这里列一下上一个请求页面的html:
<script type="text/javascript" src="js/jquery-2.1.1.js"></script> <script type="text/javascript"> $.ajax({ url: "test3", cache: false, contentType:"application/json", data:JSON.stringify({username:'张三',fav:'乒乓球'}), dataType:"json", type: "POST", success: function(html){ alert(html); } }); </script>
5、另外一种选择Gson
Gson的使用更加简单,也是目前流行的json处理包,可以把jackson替掉,引入gson
pom.xml
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency>
springMVC-servlet.xml文件
<mvc:annotation-driven> <mvc:message-converters> <ref bean="stringHttpMessageConverter"/> <ref bean="mappingGsonHttpMessageConverter" /> </mvc:message-converters> </mvc:annotation-driven> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean id="mappingGsonHttpMessageConverter" class="org.springframework.http.converter.json.GsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean>