欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot通過JSON傳遞請求參數(shù)的實例詳解

 更新時間:2022年11月24日 15:17:29   作者:IT利刃出鞘  
這篇文章主要介紹了SpringBoot通過JSON傳遞請求參數(shù),示例介紹SpringMVC如何通過JSON格式傳遞入?yún)?,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

簡介

本文用示例介紹SpringMVC如何通過JSON格式傳遞入?yún)ⅰ?/p>

JSON格式使用post方式來請求,即:對應(yīng)的注解為:@PostMapping。

@PostMapping注解的方法可以接收1個@RequestBody標記的參數(shù)和多個沒有@RequestBody標記的參數(shù)。

代碼

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把請求轉(zhuǎn)化為特定的Object(在最后會關(guān)閉相應(yīng)的流),所以在同一個方法中第二次使用@RequestBody是沒用的,因為流已經(jīng)關(guān)閉。

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參數(shù)類型是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的參數(shù)是List類型。(數(shù)組類型可以)。

到此這篇關(guān)于SpringBoot通過JSON傳遞請求參數(shù)的文章就介紹到這了,更多相關(guān)springboot傳遞請求參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 關(guān)于Java中byte[]?和?String互相轉(zhuǎn)換問題

    關(guān)于Java中byte[]?和?String互相轉(zhuǎn)換問題

    這篇文章主要介紹了Java中byte[]?和?String互相轉(zhuǎn)換問題,通過用例給大家介紹了通過String類將String轉(zhuǎn)換成byte[]或者byte[]轉(zhuǎn)換成String,具體實例代碼跟隨小編一起看看吧
    2022-01-01
  • SpringMVC實現(xiàn)數(shù)據(jù)綁定及表單標簽

    SpringMVC實現(xiàn)數(shù)據(jù)綁定及表單標簽

    這篇文章主要為大家詳細介紹了SpringMVC實現(xiàn)數(shù)據(jù)綁定及表單標簽的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • spring security中的csrf防御原理(跨域請求偽造)

    spring security中的csrf防御原理(跨域請求偽造)

    這篇文章主要介紹了spring security中的csrf防御機制原理解析(跨域請求偽造),本文通過實例代碼詳解的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • java異或加密算法

    java異或加密算法

    這篇文章主要介紹了java異或加密算法,有需要的朋友可以參考一下
    2013-12-12
  • Springboot?引入?Redis?并配置序列化并封裝RedisTemplate?

    Springboot?引入?Redis?并配置序列化并封裝RedisTemplate?

    這篇文章主要介紹了Springboot?引入?Redis?并配置序列化并封裝RedisTemplate。文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • OpenCV Java實現(xiàn)人臉識別和裁剪功能

    OpenCV Java實現(xiàn)人臉識別和裁剪功能

    這篇文章主要為大家詳細介紹了OpenCV Java實現(xiàn)人臉識別和裁剪功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Mybatis插件之自動生成不使用默認的駝峰式操作

    Mybatis插件之自動生成不使用默認的駝峰式操作

    這篇文章主要介紹了Mybatis插件之自動生成不使用默認的駝峰式操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • IDEA 配置 JRebel 熱部署的方法(推薦)

    IDEA 配置 JRebel 熱部署的方法(推薦)

    這篇文章主要介紹了IDEA 配置 JRebel 熱部署的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • java實戰(zhàn)項目之記賬軟件

    java實戰(zhàn)項目之記賬軟件

    這篇文章主要介紹了java實戰(zhàn)項目之記賬軟件,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • java中ThreadLocalRandom的使用詳解

    java中ThreadLocalRandom的使用詳解

    這篇文章主要介紹了java中ThreadLocalRandom的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03

最新評論