如何让Spring MVC接收的参数可以转换为java对象
场景:
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就可以正确的接收到参数了。
如何让Spring MVC接收的参数可以转换为java对象的更多相关文章
- 【spring mvc】后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface
后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate ...
- spring mvc接收http参数
1.http协议携带参数,无外乎两个三个存储地点:1.url上 ,2.header里 3.body里. 2.get请求是没有body的,数据全都放在url上,以?xx&xxx形式.注:get请 ...
- Spring MVC 接收前端参数的方式
方式一: 普通方式接收 1 @RequestMapping("/index") 2 public String getUserName(String username) { 3 S ...
- spring mvc 接收 put参数
web.xml中: <!-- 用户put提交参数 --> <filter> <filter-name>HttpMethodFilter</filter-nam ...
- 使用Spring mvc接收整个url地址及参数时注意事项
使用Spring mvc接收整个url地址及参数时注意事项:url= http://baidu?oid=9525c1f2b2cd45019b30a37bead6ebbb&td=2015-08- ...
- Spring MVC接收参数(Map,List,JSON,Date,2个Bean)(记录一次面试惨状)
题目Spring MVC 接收参数 MapListDate2个BeanJSON Spring MVC接收参数 -Map Spring MVC接收参数 -List Spring MVC接收参数 -dat ...
- Spring MVC url提交参数和获取参数
[转载:http://blog.csdn.net/mahoking] 普通URL提交参数 该格式url为:url.do?param1=mahc¶m2=8888.00 需 ...
- spring mvc获取路径参数的几种方式 - 浅夏的个人空间 - 开源中国社区
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- spring mvc接收ajax提交的JSON数据,并反序列化为对象
需求:spring mvc接收ajax提交的JSON数据,并反序列化为对象,代码如下: 前台JS代码: //属性要与带转化的对象属性对应 var param={name:'语文',price:16}; ...
随机推荐
- mnesia
1.模式创建 mnesia:create_schema([node()|nodes()]).集群的节点之间创建模式 2.启动和停止 application:start(mnesia). applica ...
- 37.看下图实现如下接口和类,并完成Adventure中的主方法。
//接口Swim package jieKou; public interface ICanswim { void Swim(); } //接口Fly package jieKou; public i ...
- 《TCP/IP详解》读书笔记
本书以UNIX为背景,紧贴实际介绍了数据链层.网络层.运输层 一.整体概念 1.各层协议的关系,只讨论四层 各层常见的协议: 网络层协议:IP协议.ICMP协议.ARP协议.RARP协议. ...
- Web开发人员不要错过的60款用户界面设计工具(中)
21. Dojo Dojo是一个用javascript语言实现的开源DHTML UI工具包,可实现高性能的桌面和移动应用程序开发,在国内亦有大量忠实用户. 22. Fivesecondtest Fiv ...
- Oracle技术整理(转载)
- hdu_5707_Combine String("巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5707 题意:给你三个字符串 a,b,c,问你 c能否拆成a,b,a,b串的每一个字符在c中不能变 题解 ...
- Entity Framework技巧系列之三 - Tip 9 – 12
提示9. 怎样直接删除一个对象而无需检索它 问题 最常见的删除Entity Framework中实体的方式是将你要删除的实体传入Context中并像如下这样删除: 1 // 按ID查找一个类别 2 / ...
- LeetCode OJ 220.Contains Duplicate 3
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- HDU2504:又见GCD
Problem Description 有三个正整数a,b,c(0<a,b,c<10^6),其中c不等于b.若a和c的最大公约数为b,现已知a和b,求满足条件的最小的c. Input ...
- 第13章 Swing程序设计
1.Swing概述 GUI(图形用户界面)为程序提供图形界面,最初的设计目的是为程序员构建一个通用的GUI,使其能够在所有平台上运行.但Java 1.0中基础类AWT(抽象窗口工具箱)并没有达到这个要 ...