SpringBoot通过JSON传递请求参数的实例详解
作者:IT利刃出鞘 发布时间:2023-11-26 22:59:24
简介
本文用示例介绍SpringMVC如何通过JSON格式传递入参。
JSON格式使用post方式来请求,即:对应的注解为:@PostMapping。
@PostMapping注解的方法可以接收1个@RequestBody标记的参数和多个没有@RequestBody标记的参数。
代码
Entity
User.java
package com.example.demo.entity;
import lombok.Data;
import java.util.List;
@Data
public class User {
private String name;
private Integer age;
private String[] password;
private List<Integer> scoreArray;
}
Account.java
package com.example.demo.entity;
import lombok.Data;
import java.io.Serializable;
@Data
public class Account implements Serializable {
private String phoneNum;
private String[] emails;
}
Controller
package com.example.demo.controller;
import com.example.demo.entity.User;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Arrays;
import java.util.List;
@RequestMapping("/json")
@RestController
public class JsonController {
@PostMapping("/1")
public User setUserNoAnnotation(User user, List<String> password, Integer[] scoreArray) {
printUser(user);
return user;
}
@RequestMapping("/2")
public User setUserAnnotation(@RequestBody User user) {
printUser(user);
return user;
}
@RequestMapping("/3")
public User setUserAnnotation1(@RequestBody User user, @RequestParam List<String> password, Integer[] scoreArray) {
System.out.println(password);
if (scoreArray != null) {
System.out.println(Arrays.asList(scoreArray));
} else {
System.out.println("scoreArray = null");
}
System.out.println();
printUser(user);
return user;
}
@RequestMapping("/4")
public User setUserAnnotation2(@RequestBody User user, @RequestBody List<String> password, @RequestBody Integer[] scoreArray) {
if (password != null) {
System.out.println(password);
} else {
System.out.println("password = null");
}
if (scoreArray != null) {
System.out.println(Arrays.asList(scoreArray));
} else {
System.out.println("scoreArray = null");
}
System.out.println();
printUser(user);
return user;
}
private void printUser(User user){
System.out.println("name : " + user.getName());
System.out.println("password : " + Arrays.asList(user.getPassword()));
System.out.println("scoreArray : " + user.getScoreArray());
System.out.println("acount.phoneNum : " + user.getAccount().getPhoneNum());
System.out.println("account.emails : " + Arrays.asList(user.getAccount().getEmails()));
}
}
测试
为方便测试,我用了knife4j。
测试前提
json的body
{
"name": "Jarvis",
"password": [
"ab",
"cd"
],
"scoreArray": [
99,
98
],
"account": {
"phoneNum": "123",
"emails": [
"123@qq.com",
"456@163.com"
]
}
}
正确的用法
1个RequestBody
0个@RequestBody,多个无@RequestBody
1个@RequestBody,多个无@RequestBody
错误的用法(会报错)
多个@RequestBody
后端报错信息
2022-09-20 22:04:45.857 WARN 3340 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message; nested exception is java.io.IOException: Stream closed]
错误原因
每个方法只能有一个@RequestBody。使用@RequestBody把请求转化为特定的Object(在最后会关闭相应的流),所以在同一个方法中第二次使用@RequestBody是没用的,因为流已经关闭。
You cannot use it this way as only one @RequestBody per method is allowed. Using @RequestBody Spring converts incoming request body into the specified object (what closes the stream representing body at the end) so attempting to use @RequestBody second time in the same method makes no sense as stream has been already closed.
不带@RequestBody参数类型是List
后端错误信息
2022-09-20 23:19:11.044 ERROR 3340 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: No primary or default constructor found for interface java.util.List] with root cause
java.lang.NoSuchMethodException: java.util.List.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_201]
at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_201]
...(其他信息)
错误原因
不支持非@RequstBody的参数是List类型。(数组类型可以)。
来源:https://blog.csdn.net/feiying0canglang/article/details/126955477


猜你喜欢
- synchronized都问啥?如果Java面试有什么是必问的,synchronized必定占据一席之地。初出茅庐时synchronized
- Spring 中bean的获取1.通过context.getbean 的方式来获取beanApplicationContext:是sprin
- C#支持的位逻辑运算符如表2.9所示。运算符号意义运算对象类型运算结果类型对象数实例~位逻辑非运算整型,字符型整型1~a&位逻辑与运
- 由于对运算符重载不是多么理解诶,于是就百度了一下,结果发现一个解释很有趣的百度知道,分享看看。回答:+-*/这样的运算符重定义,比如你自定义
- 本文实例讲述了WPF中不规则窗体与WindowsFormsHost控件兼容问题的解决方法。分享给大家供大家参考。具体方法如下:这里首先说明一
- 本文实例分析了Android编程之json解析的方法。分享给大家供大家参考,具体如下:JSON的定义:一种轻量级的数据交换格式,具有良好的可
- 这篇文章主要介绍了springmvc如何使用POJO作为参数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- java中不定长参数的使用方法不定长参数方法的语法如下:返回值 方法名(参数类型...参数名称)在参数列表中使用“...”形式定义不定长参数
- 一、前言null与NULL不都是表示空值吗?这有什么值得深入讨论的的?首先你在编写Java代码时使用过NULL吗?大概用IDE用习惯了,自动
- /* String name = "adsbsadgsadgtewterfsdf"
- /// <summary> /// 安装的excel的版本,0为没有安装,大于1说明安装了多个. /// </summar
- 官网下载直接打开官网:http://www.oracle.com/technetwork/java/javase/downloads/jdk
- 极少部分人运气不好可能遇到这样一个问题。只要实例化JFileChooser对象就会报空指针异常;就这一行代码出错说明不是代码的问题,应该是J
- C/C++实现扫雷小游戏源代码:github:https://github.com/KamSss/C-Practice/tree/maste
- “反射”其实就是利用程序集的元数据信息。 反射可以有很多方法,编写程序时请先导入 System.Reflection 命名空间。1、假设你要
- 折半插入排序折半插入排序是对直接插入排序的简单改进。此处介绍的折半插入,其实就是通过不断地折半来快速确定第i个元素的插入位置,这实际上是一种
- 实践过程效果代码public partial class Form1 : Form{ public Form1()
- Eureka什么是服务治理为什么需要服务治理?  服务治理是主要针对分布式服务框架的微服务,处理服务调用
- 老大让我check out 一个分支,可我在idea 右下角找了半天也没找到最后才发现:因为是刚创建的分支,我得先更新一下项目,连这个都不懂
- 前言有时候我们需要实现对照文章等,往往将文本放到两个richtextbox控件中,但是,如果我们需要同步滚动查看,来达到更好的观看效果。当然