Spring mvc处理json,我们都知道使用@ResponseBody,处理xml也是用此注解。如果想spring mvc的使用@ResponseBody注解处理json,我们需要加入一些处理bean,也可以使用默认spring提供的。
通过spring源码RequestMappingHandlerAdapter类,我们知道初始化的时候这些注解已经被加载进来,如下RequestMappingHandlerAdapter类的代码如下:argumentResolvers和returnValueHandlers一个接受一个返回各自装载很多处理,有兴趣可以研究源码。
Java代码
- @Override
- public void afterPropertiesSet() {
- if (this.argumentResolvers == null) {
- List<HandlerMethodArgumentResolver> resolvers = getDefaultArgumentResolvers();
- this.argumentResolvers = new HandlerMethodArgumentResolverComposite().addResolvers(resolvers);
- }
- if (this.initBinderArgumentResolvers == null) {
- List<HandlerMethodArgumentResolver> resolvers = getDefaultInitBinderArgumentResolvers();
- this.initBinderArgumentResolvers = new HandlerMethodArgumentResolverComposite().addResolvers(resolvers);
- }
- if (this.returnValueHandlers == null) {
- List<HandlerMethodReturnValueHandler> handlers = getDefaultReturnValueHandlers();
- this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite().addHandlers(handlers);
- }
- initControllerAdviceCache();
- }
言归正传,下面介绍处理json
spring配置文件如下:
Java代码
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd"
- default-lazy-init="false">
-
- <!-- mvc:annotation-driven使用注解功能 -->
- <mvc:annotation-driven>
- <mvc:message-converters register-defaults="true">
- <!--json处理-->
- <bean
- class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>application/json;charset=UTF-8</value>
- <value>text/html;charset=UTF-8</value>
- </list>
- </property>
- </bean>
- </mvc:message-converters>
- </mvc:annotation-driven>
-
- <!-- 只扫描Controller 注解 -->
- <context:component-scan base-package="com.zx.web"
- use-default-filters="false">
- <context:include-filter type="annotation"
- expression="org.springframework.stereotype.Controller" />
- </context:component-scan>
-
- <!-- 动态资源访问,默认使用其他handlerAdapter解析 -->
- <mvc:resources mapping="/images/**" location="/images/" />
- <mvc:resources mapping="/js/**" location="/js/" />
- <mvc:resources mapping="/css/**" location="/css/" />
-
-
- <!-- jsp视图 -->
- <bean id="jspViewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView" />
- <property name="prefix" value="/jsp/" />
- <property name="suffix" value=".jsp" />
- <property name="order" value="0" />
- </bean>
-
-
- </beans>
我使用的是spring自带的类MappingJackson2HttpMessageConverter处理,在spring4.1以前都是使用MappingJacksonHttpMessageConverter类处理,在spring4.1以后都是用MappingJackson2HttpMessageConverter处理,然而换了类,自然依赖的jackson也进行升级了,spring4,1以后使用jackson-annotations-2.5.0.jar,jackson-core-2.5.0.jar,jackson-databind-2.5.0.jar
Controller层代码如下:
Java代码
- package com.zx.web;
-
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- import com.zx.entity.User;
-
- @org .springframework.web.bind.annotation.RestController
- public class RestController {
-
- @RequestMapping("/rest")
- @ResponseBody
- public User getUser()
- {
- User user = new User();
- user.setName("nihao");
- user.setAddress("anhui");
- user.setAge("89");
- user.setWork("软件");
-
- return user;
- }
- }
结果如下:
spring web 4.1处理json
原文:http://my.oschina.net/u/2273085/blog/476790