最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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傳遞請求參數內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • feign 調用第三方服務中部分特殊符號未轉義問題

    feign 調用第三方服務中部分特殊符號未轉義問題

    這篇文章主要介紹了feign 調用第三方服務中部分特殊符號未轉義問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java Swing中JDialog實現用戶登陸UI示例

    Java Swing中JDialog實現用戶登陸UI示例

    這篇文章主要介紹了Java Swing中JDialog實現用戶登陸UI功能,結合完整實例形式分析了Swing使用JDialog實現用戶登陸UI界面窗口功能的步驟與相關操作技巧,需要的朋友可以參考下
    2017-11-11
  • springboot項目中添加自定義日志及配置過程

    springboot項目中添加自定義日志及配置過程

    這篇文章主要介紹了springboot項目中添加自定義日志,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • JavaSE學習之內部類及常用API

    JavaSE學習之內部類及常用API

    這篇文章主要介紹了JavaSE中的內部類和幾個常用的API,文中的示例代碼介紹詳細,對我們學習JavaSEI有一定的幫助,感興趣的小伙伴可以跟隨小編一起學習一下
    2021-12-12
  • SpringCloud Feign參數問題及解決方法

    SpringCloud Feign參數問題及解決方法

    這篇文章主要介紹了SpringCloud Feign參數問題及解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • 解決Error:(5, 28) java: 程序包org.apache.ibatis.io不存在問題

    解決Error:(5, 28) java: 程序包org.apache.ibatis.io

    這篇文章主要介紹了解決Error:(5, 28) java: 程序包org.apache.ibatis.io不存在問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • SpringBoot POST請求接收多個參數值為null問題

    SpringBoot POST請求接收多個參數值為null問題

    SpringBoot接口中POST請求接收JSON數據時,使用簡單類型接收會報null,需要封裝成實體類或使用Map(并注明泛型)接收,并且使用@RequestBody注解
    2025-02-02
  • java Zookeeper簡述

    java Zookeeper簡述

    ZooKeeper是一個分布式的,開放源碼的分布式應用程序協調服務,是Google的Chubby一個開源的實現,是Hadoop和Hbase的重要組件。下面通過本文給大家分享java 中 zookeeper簡單使用,需要的朋友參考下吧
    2021-09-09
  • SpringBoot+Hutool+thymeleaf完成導出Excel的實現方法

    SpringBoot+Hutool+thymeleaf完成導出Excel的實現方法

    這篇文章主要介紹了SpringBoot+Hutool+thymeleaf完成導出Excel,本篇示例當中不僅僅有后端,而且還提供了前端html,html當中利用js將后端 輸出流直接下載為文件,需要的朋友可以參考下
    2022-03-03
  • Mybatis配置之typeAlias標簽的用法

    Mybatis配置之typeAlias標簽的用法

    這篇文章主要介紹了Mybatis配置之typeAlias標簽的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評論

常山县| 重庆市| 利津县| 绥德县| 宝山区| 沛县| 清新县| 彝良县| 黄石市| 繁峙县| 西吉县| 疏勒县| 伽师县| 泗水县| 长丰县| 宁海县| 六枝特区| 张北县| 安多县| 普兰店市| 林芝县| 汤原县| 册亨县| 柳林县| 淳化县| 开封县| 新绛县| 莱芜市| 乐山市| 永宁县| 商水县| 凤城市| 尼木县| 遂宁市| 朝阳区| 安乡县| 乌拉特后旗| 蓝山县| 巨鹿县| 怀集县| 宽甸|