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

Springboot 接收POST、json、文本數(shù)據(jù)的方法 附示例

 更新時(shí)間:2023年10月12日 11:36:30   作者:梅塢茶坊  
這篇文章主要介紹了Springboot 接收POST、json、文本數(shù)據(jù)實(shí)踐,如果把 json 作為參數(shù)傳遞,我們可以使用 @requestbody 接收參數(shù),將數(shù)據(jù)直接轉(zhuǎn)換成對(duì)象,本文通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

一、接收 Form 表單數(shù)據(jù)

1,基本的接收方法

(1)下面樣例 Controller 接收 form-data 格式的 POST 數(shù)據(jù):

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
    @PostMapping("/postHello1")
    public String postHello1(@RequestParam("name") String name,
                        @RequestParam("age") Integer age) {
        return "name:" + name + "\nage:" + age;
    }
}

(2)下面是一個(gè)簡(jiǎn)單的測(cè)試樣例:

2,參數(shù)沒(méi)有傳遞的情況

(1)如果沒(méi)有傳遞參數(shù) Controller 將會(huì)報(bào)錯(cuò),這個(gè)同樣有如下兩種解決辦法:

  • 使用 required = false 標(biāo)注參數(shù)是非必須的。
  • 使用 defaultValue 給參數(shù)指定個(gè)默認(rèn)值。
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
    @PostMapping("/postHello2")
    public String postHello2(@RequestParam(name = "name", defaultValue = "xxx") String name,
                        @RequestParam(name = "age", required = false) Integer age) {
        return "name:" + name + "\nage:" + age;
    }
}

3,使用 map 來(lái)接收參數(shù)

(1)Controller 還可以直接使用 map 來(lái)接收所有的請(qǐng)求參數(shù):

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class HelloController {
    @PostMapping("/postHello3")
    public String postHello3(@RequestParam Map<String,Object> params) {
        return "name:" + params.get("name") + "\nage:" + params.get("age");
    }
}

(2)下面是一個(gè)簡(jiǎn)單的測(cè)試樣例:

4,接收一個(gè)數(shù)組

(1)表單中有多個(gè)同名參數(shù),Controller 這邊可以定義一個(gè)數(shù)據(jù)進(jìn)行接收:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class HelloController {
    @PostMapping("/postHello4")
    public String postHello4(@RequestParam("name") String[] names) {
        String result = "";
        for(String name:names){
            result += name + "\n";
        }
        return result;

(2)下面是一個(gè)簡(jiǎn)單的測(cè)試樣例:

5,使用對(duì)象來(lái)接收參數(shù)

1)如果一個(gè) post 請(qǐng)求的參數(shù)太多,我們構(gòu)造一個(gè)對(duì)象來(lái)簡(jiǎn)化參數(shù)的接收方式:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
    @PostMapping("/postHello5")
    public String postHello5(User user) {
        return "name:" + user.getName() + "\nage:" + user.getAge();
    }
}

(2)User 類(lèi)的定義如下,到時(shí)可以直接將多個(gè)參數(shù)通過(guò) getter、setter 方法注入到對(duì)象中去:

public class User {
    private String name;
    private Integer age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }

(3)下面是一個(gè)簡(jiǎn)單的測(cè)試樣例:

(4)如果傳遞的參數(shù)有前綴,且前綴與接收實(shí)體類(lèi)的名稱(chēng)相同,那么參數(shù)也是可以正常傳遞的:

(5)如果一個(gè) post 請(qǐng)求的參數(shù)分屬不同的對(duì)象,也可以使用多個(gè)對(duì)象來(lái)接收參數(shù):

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
    @PostMapping("/postHello5-1")
    public String hello(User user, Phone phone) {
        return "name:" + user.getName() + "\nage:" + user.getAge()
                + "\nnumber:" + phone.getNumber();
    }
}

6,使用對(duì)象接收時(shí)指定參數(shù)前綴

(1)如果傳遞的參數(shù)有前綴,且前綴與接收實(shí)體類(lèi)的名稱(chēng)不同相,那么參數(shù)無(wú)法正常傳遞:

(2)我們可以結(jié)合 @InitBinder 解決這個(gè)問(wèn)題,通過(guò)參數(shù)預(yù)處理來(lái)指定使用的前綴為 u. 

 除了在 Controller 里單獨(dú)定義預(yù)處理方法外,我們還可以通過(guò) @ControllerAdvice 結(jié)合 @InitBinder 來(lái)定義全局的參數(shù)預(yù)處理方法,方便各個(gè) Controller 使用。具體做法參考我之前的文章:

SpringBoot - @ControllerAdvice的使用詳解3(請(qǐng)求參數(shù)預(yù)處理 @InitBinder)

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
@RestController
public class HelloController {
    @PostMapping("/postHello6")
    public String postHello6(@ModelAttribute("u") User user) {
        return "name:" + user.getName() + "\nage:" + user.getAge();
    }
    @InitBinder("u")
    private void initBinder(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("u.");
    }
}

(3)重啟程序再次發(fā)送請(qǐng)求,可以看到參數(shù)已經(jīng)成功接收了:

二、接收字符串文本數(shù)據(jù)

(1)如果傳遞過(guò)來(lái)的是 Text 文本,我們可以通過(guò) HttpServletRequest 獲取輸入流從而讀取文本內(nèi)容。

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@RestController
public class HelloController {
    @PostMapping("/helloText")
    public String hello(HttpServletRequest request) {
        ServletInputStream is = null;
        try {
            is = request.getInputStream();
            StringBuilder sb = new StringBuilder();

三、接收 JSON 數(shù)據(jù)

1,使用 Map 來(lái)接收數(shù)據(jù)

(1)如果把 json 作為參數(shù)傳遞,我們可以使用 @requestbody 接收參數(shù),將數(shù)據(jù)轉(zhuǎn)換 Map:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class HelloController {
    @PostMapping("/helloMap")
    public String helloMap(@RequestBody Map params) {
        return "name:" + params.get("name") + "\n age:" + params.get("age");
    }
}

(2)下面是一個(gè)簡(jiǎn)單的測(cè)試樣例:

2,使用 Bean 對(duì)象來(lái)接收數(shù)據(jù)

(1)如果把 json 作為參數(shù)傳遞,我們可以使用 @requestbody 接收參數(shù),將數(shù)據(jù)直接轉(zhuǎn)換成對(duì)象:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
    @PostMapping("/helloBean")
    public String hello(@RequestBody User user){
        return user.getName() + " " + user.getAge();
    }
}

(2)下面是一個(gè)簡(jiǎn)單的測(cè)試樣例:

(4)如果傳遞的 JOSN 數(shù)據(jù)是一個(gè)數(shù)組也是可以的,Controller 做如下修改:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class HelloController {
    @PostMapping("/helloList")
    public String helloList(@RequestBody List<User> users){
        String result = "";
        for(User user:users){
            result += user.getName() + " " + user.getAge() + "\n";
        }
        return result;

相關(guān)文章

  • Java如何實(shí)現(xiàn)對(duì)稱(chēng)加密

    Java如何實(shí)現(xiàn)對(duì)稱(chēng)加密

    這篇文章主要介紹了Java如何實(shí)現(xiàn)對(duì)稱(chēng)加密問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Spring IOC基于注解啟動(dòng)示例詳析

    Spring IOC基于注解啟動(dòng)示例詳析

    這篇文章主要給大家介紹了Spring IOC基于注解啟動(dòng)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Java最簡(jiǎn)單的DES加密算法實(shí)現(xiàn)案例

    Java最簡(jiǎn)單的DES加密算法實(shí)現(xiàn)案例

    下面小編就為大家?guī)?lái)一篇Java最簡(jiǎn)單的DES加密算法實(shí)現(xiàn)案例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • 解決okhttp3提示java.lang.IllegalStateException:closed異常問(wèn)題

    解決okhttp3提示java.lang.IllegalStateException:closed異常問(wèn)題

    使用OkHttp3時(shí),response.body().string()只能調(diào)用一次,重復(fù)調(diào)用會(huì)導(dǎo)致流關(guān)閉異常,解決方法是保存響應(yīng)體內(nèi)容到變量或重新構(gòu)建Response對(duì)象,避免多次讀取
    2025-07-07
  • Java實(shí)現(xiàn)圖片上文字內(nèi)容的動(dòng)態(tài)修改的操作步驟

    Java實(shí)現(xiàn)圖片上文字內(nèi)容的動(dòng)態(tài)修改的操作步驟

    在數(shù)字圖像處理領(lǐng)域,Java提供了強(qiáng)大的庫(kù)來(lái)處理圖片,包括讀取、修改和寫(xiě)入圖片,如果你需要在Java應(yīng)用程序中修改圖片上的文字內(nèi)容,可以通過(guò)圖像處理技術(shù)來(lái)實(shí)現(xiàn),這篇博文將介紹如何使用Java實(shí)現(xiàn)圖片上文字內(nèi)容的動(dòng)態(tài)修改,需要的朋友可以參考下
    2024-07-07
  • 帶你快速入門(mén)掌握Spring的那些注解使用

    帶你快速入門(mén)掌握Spring的那些注解使用

    注解是個(gè)好東西,注解是Java語(yǔ)法,被Java編譯器檢查,可以減少配置錯(cuò)誤,這篇文章主要給大家介紹了關(guān)于Spring的那些注解使用的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • java設(shè)計(jì)模式之單例模式

    java設(shè)計(jì)模式之單例模式

    這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式之單例模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Java數(shù)組索引異常產(chǎn)生及解決方案

    Java數(shù)組索引異常產(chǎn)生及解決方案

    這篇文章主要介紹了Java數(shù)組索引異常產(chǎn)生及解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Java通俗易懂系列設(shè)計(jì)模式之責(zé)任鏈模式

    Java通俗易懂系列設(shè)計(jì)模式之責(zé)任鏈模式

    這篇文章主要介紹了Java通俗易懂系列設(shè)計(jì)模式之責(zé)任鏈模式,對(duì)設(shè)計(jì)模式感興趣的同學(xué),一定要看一下
    2021-04-04
  • Java I/O中I/O流的典型使用方式詳解

    Java I/O中I/O流的典型使用方式詳解

    這篇文章主要介紹了Java I/O中I/O流的典型使用方式詳解,盡管可以通過(guò)不同的方式組合IO流類(lèi),但我們可能也就只用到其中的幾種組合。下面的例子可以作為典型的IO用法的基本參考,,需要的朋友可以參考下
    2019-06-06

最新評(píng)論

噶尔县| 湟源县| 通江县| 宜川县| 扎赉特旗| 菏泽市| 贵溪市| 依安县| 固原市| 元氏县| 怀化市| 梅州市| 宜春市| 漯河市| 南康市| 固阳县| 错那县| 四会市| 乐清市| 高州市| 农安县| 亚东县| 池州市| 福清市| 鄂伦春自治旗| 沧州市| 丽水市| 丰都县| 讷河市| 项城市| 桦甸市| 兖州市| 阳信县| 大同县| 鞍山市| 房山区| 平泉县| 平利县| 星座| 洛浦县| 班玛县|