springmvc参数为对象,数组的操作
作者:shuair 时间:2022-04-20 07:38:03
参数为对象
1、提交表单
2、表单序列化,使用ajax提交
var data = $("#addForm").serialize();
$.ajax({
url : "addReportDo", //请求url
type : "POST", //请求类型 post|get
data : data,
dataType : "text", //返回数据的 类型 text|json|html--
success : function(result){ //回调函数 和 后台返回的 数据
alert(result);
}
});
3、也可以这样写
var data = {
title: $("#title").val(),
note: $("#note").val()
};
4、如果结构复杂,使用@RequestBody
需要引用jackson
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
springmvc.xml配置
<!--Spring3.1开始的注解 HandlerAdapter -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<!-- json转换器 -->
<property name="messageConverters">
<list>
<ref bean="mappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>text/json;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
js写法
var goods1 = {
goodsNumber: "001",
goodsName: "商品A"
}
var goods2 = {
goodsNumber: "002",
goodsName: "商品B"
}
var goodsList = [goods1,goods2];
var data = {
title: $("#title").val(),
note: $("#note").val(),
goodsList: goodsList
};
console.log(data);
$.ajax({
url : "addReportDo", //请求url
type : "POST", //请求类型 post|get
data : JSON.stringify(data),
contentType : "application/json",
dataType : "text", //返回数据的 类型 text|json|html--
success : function(result){ //回调函数 和 后台返回的 数据
alert(result);
}
});
注意ajax的两个属性,data属性变为JSON.stringify(data),增加contentType属性。
controller代码写法
@ResponseBody
@RequestMapping("addReportDo")
public String addReportDo(@RequestBody Report report){
System.out.println(report);
return "ok";
}
在参数前面加上@RequestBody即可。
5、传递数组
js写法
var array = ["a","b","c"];
var data = {
array : array
};
console.log(data);
$.ajax({
url : "addReportDo", //请求url
type : "POST", //请求类型 post|get
data : data,
dataType : "text", //返回数据的 类型 text|json|html--
success : function(result){ //回调函数 和 后台返回的 数据
alert(result);
}
});
controller写法
@ResponseBody
@RequestMapping("addReportDo")
public String addReportDo(@RequestParam("array[]") String[] array){
System.out.println(Arrays.toString(array));
return "ok";
}
也可以用List接收
@ResponseBody
@RequestMapping("addReportDo")
public String addReportDo(@RequestParam("array[]") List<String> list){
System.out.println(list);
return "ok";
}
springmvc接受复杂对象(对象数组)
前端:
将请求头改为
contentType:"application/json;charset=UTF-8"
后端:
自定义一个对象,将参数封装进该对象中
@Data
public class CaseBodyEntity {
String token;
CaseBasicModel caseBasic;
String[] keywords;
CaseInsurantAndProductModel[] caseInsurantAndProductModels;
CaseExperienceModel[] caseExperiences;
CaseAssessModel[] caseAssesses;
}
使用使用POST方式接受请求,@RequestBody接受请求参数,对象为自定义的接受对象
@ApiOperation("添加或更新案例,后台")
@PostMapping("/addOrUpdateCase")
public JSONObject addOrUpdateCase(
@RequestBody CaseBodyEntity caseBodyEntity
) {
...
}
@RequestBody和@RequestParam的区别
@RequestParam,主要处理contentType为application/x-www-form-urlencoded的数据(默认);@ResponseBody:主要处理contentType不为application/x-www-form-urlencoded的数据,例如:application/json;charset=UTF-8
@RequestParam:要指明前端传过来的参数名并与其对应;@RequestBody:直接对象接收,属性名要与前端传过来的数据的key值对应
使用@RequestParam:Content-Type为application/x-www-form-urlencoded,参数在FormData中;使用@RequestBody:Content-Type为application/json,参数在Request PayLoad中
可以使用多个@RequestParam获取数据;@RequestBody不能在同一个方法中出现多次
来源:https://blog.csdn.net/shuair/article/details/81212603
标签:springmvc,参数,对象,数组
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何利用java控制鼠标操作一些重复的事情
2021-11-12 12:54:02
![](https://img.aspxhome.com/file/2023/6/64016_0s.jpg)
深入解析JVM之内存结构及字符串常量池(推荐)
2023-01-02 19:48:20
![](https://img.aspxhome.com/file/2023/9/66469_0s.png)
Java实现在线五子棋对战游戏(人机对战)
2023-01-10 19:07:29
![](https://img.aspxhome.com/file/2023/6/74896_0s.gif)
对比Java中的Comparable排序接口和Comparator比较器接口
2023-10-29 04:29:51
ShardingSphere jdbc实现分库分表核心概念详解
2023-11-24 12:09:45
![](https://img.aspxhome.com/file/2023/3/59763_0s.png)
Java建造者设计模式详解
2022-09-19 13:14:11
![](https://img.aspxhome.com/file/2023/6/71236_0s.jpg)
Java代码实现循环队列的示例代码
2023-11-23 23:51:25
![](https://img.aspxhome.com/file/2023/7/59927_0s.jpg)
利用Springboot+Caffeine实现本地缓存实例代码
2022-05-31 09:13:33
java编写Http服务器下载工具
2021-11-08 08:07:38
详解Spring Boot加载properties和yml配置文件
2023-11-24 07:14:09
Java数据结构BFS广搜法解决迷宫问题
2023-12-03 16:04:33
![](https://img.aspxhome.com/file/2023/4/63304_0s.png)
如何用java实现分页查询
2023-09-23 06:50:24
应用启动数据初始化接口CommandLineRunner和Application详解
2023-02-06 05:00:33
![](https://img.aspxhome.com/file/2023/8/65608_0s.jpg)
浅谈c++性能测试工具之计算时间复杂度
2023-07-09 12:40:05
![](https://img.aspxhome.com/file/2023/4/79524_0s.jpg)
springcloud干货之服务注册与发现(Eureka)
2021-07-08 11:04:23
![](https://img.aspxhome.com/file/2023/1/70961_0s.png)
springboot 使用QQ邮箱发送邮件的操作方法
2022-03-03 14:36:22
![](https://img.aspxhome.com/file/2023/8/62218_0s.png)
利用logback 设置不同包下的日志级别
2022-08-11 20:05:43
SpringBoot 整合 ElasticSearch操作各种高级查询搜索
2023-03-25 17:12:40
SpringBoot2.x过后static下的静态资源无法访问的问题
2023-07-07 00:21:09
![](https://img.aspxhome.com/file/2023/3/67623_0s.jpg)
Android ViewDragHelper使用介绍
2023-08-01 04:18:59
![](https://img.aspxhome.com/file/2023/8/85458_0s.gif)