- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
- <property name="messageConverters">
- <list>
- <bean
- class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>text/html;charset=UTF-8</value>
- <value>text/plain;charset=UTF-8</value>
- <value>application/json;charset=UTF-8</value>
- </list>
- </property>
- </bean>
- </list>
- </property>
- </bean>
HttpMessageConverter<T>的实现类有如下几个:
<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
StringHttpMessageConverter:将请求信息转换为字符串,T为String,可读取所有媒体类型(*/*)
ByteArrayHttpMessageConverter:读写二进制数据,T为byte[]类型,可读取*/*,可通过supportedMediaTypes属性指定媒体类型,响应信息媒体类型为application/octer-stream
MappingJackson2HttpMessageConverter:利用jackson的ObjectMapper读写Json数据,T为Object,默认可读取application/json,响应媒体类型为application/json可通过supportedMediaTypes属性指定媒体类型,如上面代码中指定了三个MIME类型text/html、text/plain、application/json,并且可以指定编码类型,所以如果遇到乱码的情况可以来检查此处的编码配置是否正确。
MappingJackson2HttpMessageConverter
千篇一律不如重点说一个MappingJackson2HttpMessageConverter,这个类的主要功能就是实现json的自动转换化,spring-mvc.xml的配置如上面所示。此外需要添加一下Jackson的依赖:
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-annotations</artifactId>
- <version>2.5.0</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-core</artifactId>
- <version>2.5.0</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.5.0</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.jr</groupId>
- <artifactId>jackson-jr-all</artifactId>
- <version>2.5.0</version>
- </dependency>
- @RequestMapping(value = "/accountLogin", method = RequestMethod.POST)
- @ResponseBody
- public FirstResposeBean accountLogin(@RequestBody PostUserBean postBean) {
- FirstResposeBean resposeBean = new FirstResposeBean();
- resposeBean.setErrorCode(ErrorCode.SUCCESS_CODE);
- resposeBean.setMsg("登录成功!" );
- resposeBean.setData(postBean.getUserInfo().getName());
- return resposeBean;
- }
springmvc的处理过程
1、springmvc将根据requestBody的类型查找匹配的HttpMessageConverter,比如我在客户端做如下设置:request.setHeader("Content-Type","application/json; charset=utf-8");
2、那么这个请求的请求体body就会被springmvc匹配给我们上面设置的MappingJackson2HttpMessageConverter来处理。请求的到body内容如下:{"timeStamp":"1442295420941","userInfo":{"password":"ff","name":"路路通"},"nonceStr":"08a18993-6c87-407e-b0db-50538b9b2e4f"}
此外StringHttpMessageConverter也可以配置MIME类型为application/json,所以匹配的依据不仅是Content-Type,@RequestBody所指定参数的数据类型也是依据之一。
MappingJackson2HttpMessageConverter会自动把上面的json数据解析成PostUserBean类,并且把解析结果赋值给controller中@RequestBody标记的参数postBean,由此可见json的解析过程完全不用我们操心。
3、此外,accountLogin方法是由@ResponseBody标记的,返回数据类型是自定义的FirstResposeBean,这时候springmvc依然会让MappingJackson2HttpMessageConverter自动把FirstResposeBean转换成json数据返回,转换后的数据如下:
{"errorCode":1000000,"msg":"登录成功!","data":"路路通"}
至此Java类到json的转换过程我们也是不需要操心了。
需要提醒的是,并不是任何情况都采用自动转换的形式都最好,特殊情况下我们需要自己对数据进行处理,这时候我们可以选择如下方式:
- ObjectMapper mapper = new ObjectMapper();
- List<UserBean> userList = mapper.readValue(strJson,
- new TypeReference<List<UserBean>>() {
- });
另外需要说明的是如果你不想把值为null的属性加入到json中,可以使用@JsonInclude(Include.NON_NULL)标记你的bean:
- @JsonInclude(Include.NON_NULL)
- public class UserBean {
- private String password;
- private String name;
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }