maven与springMVC之HttpMessageConverter解析json
来源:互联网 发布:法国小哥知乎 编辑:程序博客网 时间:2024/05/18 02:15
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <!--<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">--> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json</value> <value>text/json</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;
- }
- }
- maven与springMVC之HttpMessageConverter解析json
- maven与springMVC之HttpMessageConverter解析json
- maven与springMVC之HttpMessageConverter解析json
- maven与springMVC之HttpMessageConverter解析json
- SpringMVC之HttpMessageConverter
- springmvc之HttpMessageConverter
- SpringMVC处理Json-使用 HttpMessageConverter
- SpringMVC处理Json-使用 HttpMessageConverter
- springmvc之HttpMessageConverter<T>接口
- springMVC 处理json 及 HttpMessageConverter 接口
- SpringMVC之HttpMessageConverter&RestTemplate笔记(二)
- SpringMVC 学习笔记(七) JSON返回:HttpMessageConverter作用
- SpringMVC 学习笔记(七) JSON返回:HttpMessageConverter作用
- maven与springMVC之Controller
- springmvc之json数据的解析
- SpringMVC之配置用于@ResponseBody的HttpMessageConverter转换器
- SpringMVC 中 HttpMessageConverter<T>
- [SpringMVC] HttpMessageConverter配置
- 中断等待和异常处理优化
- Unity3D Update() 和 FixedUpdate()区别
- Python学习笔记:二
- 让VS2010调试器帮你格式化显示自定义数据
- git 删除分支
- maven与springMVC之HttpMessageConverter解析json
- 微信小程序之----video视频播放
- UnsatisfiedLinkError错误的原因以及解决方案
- #笔记1
- Java中实现序列化的两种方式 Serializable 接口和 Externalizable接口
- RDD基本转换操作(6)–zip、zipPartitions
- JS遍历后台传过来的JSON数据
- (POJ 3660)Cow Contest [详解] [floyd] 传递闭包 确定top序列顺序
- IIS 超简单部署免费https Let's Encrypt