场景:
web.xml中增加了一个DispatcherServlet配置,并在同级目录下添加了**-servlert.xml文件,搭建起了一个spring mvc的restful访问接口。
问题描述:
Controller的@RequestBody,
如果参数定义类型为String,则可以获取到数据;
如果参数定义类型为其他java对象,就接收不到。
下面记录完整的解决方法:
1. web.xml
<!-- spring mvc依赖的大环境,此参数会被ContextLoaderListener使用 -->
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicatonContext.xml</param-value> </context-param>
<!-- 模块化接口的配置 -->
<servlet> <servlet-name>rest</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/rest-servlet.xml</param-value> <!-- 模块化接口的配置文件位置,此处填写的是默认地址,也可指向其他位置 --> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>rest</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>
2. rest-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <mvc:annotation-driven /> <!-- 开启注解扫描 --> <context:component-scan base-package="com.springmvc.rest" /> <!-- 指定需要扫描的package --> </beans>
3. applicatonContext.xml
添加如下的一段配置:
作用:指定json类型参数的转换器,可将接收到的json类型的参数,封装为java对象。(去掉这个配置就会报错)
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager"/> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="jsonHttpMessageConverter" /> </list> </property> </bean> <bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean>
4. pom.xml
添加三个jackson依赖jar包。(spring MVC的jar,也需添加依赖,此处不描述)
作用:spring mvc底层解析json成java类,用的就是jsckson的jar包,不依赖它肯定也是无法成功解析的啦。
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.1</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-lgpl</artifactId> <version>1.8.1</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-lgpl</artifactId> <version>1.8.1</version> </dependency>
5. Controller的方法
指定接收参数类型为application/json。(我把这个指定去掉也正确接收了,还是加上吧)
@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = "application/json") public @ResponseBody RequestResult test(@RequestBody User user) { // .... }
至此,@RequestBody就可以正确的接收到参数了。