SpringBoot通过JSON传递请求参数的实例详解

 更新时间:2022年11月24日 15:17:29   作者:IT利刃出鞘  
这篇文章主要介绍了SpringBoot通过JSON传递请求参数,示例介绍SpringMVC如何通过JSON格式传递入参,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

简介

本文用示例介绍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类型。(数组类型可以)。

到此这篇关于SpringBoot通过JSON传递请求参数的文章就介绍到这了,更多相关springboot传递请求参数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java实现二分法查找出数组重复数字

    java实现二分法查找出数组重复数字

    这篇文章主要为大家详细介绍了java实现二分法查找出数组重复数字,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-11-11
  • Mybatis实体类和表映射问题(推荐)

    Mybatis实体类和表映射问题(推荐)

    在项目开发中我们经常会遇到表中的字段名和表对应实体类的属性名称不一定都是完全相同的。下面小编给大家介绍下这种情况下如何解决字段名与实体类属性名不相同的冲突问题。下面小编给大家带来了Mybatis实体类和表映射的解决方法,小伙伴们一起学习吧
    2016-09-09
  • springboot如何实现国际化配置

    springboot如何实现国际化配置

    这篇文章主要介绍了springboot如何实现国际化配置问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • 一文详解Java中枚举类的使用

    一文详解Java中枚举类的使用

    这篇文章主要介绍了深入浅出讲解Java中的枚举类,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,感兴趣的朋友可以了解下
    2022-11-11
  • Java开发之内部类对象的创建及hook机制分析

    Java开发之内部类对象的创建及hook机制分析

    这篇文章主要介绍了Java开发之内部类对象的创建及hook机制,结合实例形式分析了java基于hook机制内部类对象的创建与使用,需要的朋友可以参考下
    2018-01-01
  • 关于接口ApplicationContext中的getBean()方法使用

    关于接口ApplicationContext中的getBean()方法使用

    这篇文章主要介绍了关于接口ApplicationContext中的getBean()方法使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • SpringBoot将Bean放入容器的五种方式

    SpringBoot将Bean放入容器的五种方式

    这篇文章给大家介绍了SpringBoot将Bean放入容器的五种方式,文中通过代码示例给大家介绍的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-02-02
  • netty服务端辅助类ServerBootstrap创建逻辑分析

    netty服务端辅助类ServerBootstrap创建逻辑分析

    这篇文章主要介绍了netty服务端辅助类ServerBootstrap创建逻辑分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-03-03
  • java字符串如何只保留数字、字母、中文

    java字符串如何只保留数字、字母、中文

    这篇文章主要介绍了java字符串如何只保留数字、字母、中文问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • 解决SpringMVC使用@RequestBody注解报400错误的问题

    解决SpringMVC使用@RequestBody注解报400错误的问题

    这篇文章主要介绍了解决SpringMVC使用@RequestBody注解报400错误的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09

最新评论