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

SpringBoot優(yōu)雅接收前端請求參數(shù)的詳細過程

 更新時間:2023年06月26日 15:45:49   作者:新垣不結(jié)衣  
這篇文章主要介紹了SpringBoot如何優(yōu)雅接收前端請求參數(shù),我們可以通過@RequestParm注解去綁定請求中的參數(shù),將(查詢參數(shù)或者form表單數(shù)據(jù))綁定到controller的方法參數(shù)中,本文結(jié)合示例代碼給大家講解的非常詳細,需要的朋友可以參考下

@RequestParm

我們可以通過@RequestParm注解去綁定請求中的參數(shù),將(查詢參數(shù)或者form表單數(shù)據(jù))綁定到controller的方法參數(shù)中,通俗點說就是,我們可以在get請求和post請求中使用改注解,get請求中會從查詢參數(shù)中獲取參數(shù),post請求會從form表單或者查詢參數(shù)中獲取參數(shù)

  • 默認情況下,方法參數(shù)上使用該注解說明這個參數(shù)是必傳的,但是我們可以通過required = false來設(shè)置是否必傳,或者通過defaultValue設(shè)置默認值。
  • 如果類型不是Sting類型的話,會自動進行轉(zhuǎn)換
  • 可以將參數(shù)類型聲明為list,同一個參數(shù)名解析多個參數(shù)值

1.使用Get請求

    /**
     *GET http://localhost:8080/api/person/getPerson?id=1
     */
    @GetMapping("/getPerson")
    public String getPerson(@RequestParam(value = "id",required = false,defaultValue = "2") Integer id) {
        log.info("id:{}", id);
        return "";
    }

2.使用Post請求

    /**
     * POST http://localhost:8080/api/person/postPerson
     * Content-Type: application/x-www-form-urlencoded
     *
     * id=1
     */
    @PostMapping("/postPerson")
    public String postPerson(@RequestParam("id") String id) {
        log.info("id:{}", id);
        return "";
    }

3.使用Map接收

    /**
     *POST http://localhost:8080/api/person/postPersonMap
     * Content-Type: application/x-www-form-urlencoded
     *
     * id=1
     */
    @PostMapping("/postPersonMap")
    public String postPersonMap(@RequestParam HashMap<String,String> param) {
        log.info("param:{}",param.toString());
        return "";
    }

4.使用List接收

    /**
     *可以通過兩種方式傳遞list
     * 第一種傳遞到param中:
     * POST http://localhost:8080/api/person/postPersonList?param=1,2,3,4
     *
     * 第二種傳遞到form表單中:
     * POST http://localhost:8080/api/person/postPersonList
     * Content-Type: application/x-www-form-urlencoded
     *
     * param=1,2,3,4,5
     */
    @PostMapping("/postPersonList")
    public String postPersonList(@RequestParam List<String> param) {
        log.info("param:{}",param);
        return "";
    }

5.什么都不加

? 如果不加的話,前端可以不傳id。默認:get請求通過param傳遞,post請求通過form表單傳遞

    /**
     * GET http://localhost:8080/api/person/getPerson?id=1
     */
    @GetMapping("/getPersonById")
    public String getPersonById(Integer id) {
        log.info("id:{}", id);
        return "";
    }

@RequestBody

@RequestBody主要用來接收前端傳遞給后端的json字符串中的數(shù)據(jù)的(請求體中的數(shù)據(jù)的);

如果參數(shù)未傳的話,引用類型默認是null,基本類型為默認值

注:一個請求,只有一個RequestBody;一個請求,可以有多個RequestParam。

### test postPersonByJson
POST http://localhost:8080/api/person/postPersonByJson
Content-Type: application/json
{
  "id": 1,
  "name": "tom",
  "age": 12,
  "ADDRESS": "beijing"
}

默認json的key和屬性名稱必須要匹配的,并且大小寫敏感

如果有別名的話可以使用@JsonAlias注解或者使用@JsonProperty注解如下:

    /**
     * 地址
     */
    @JsonAlias(value = {"ADDRESS"})
    private String adress;
    /**
     * 地址
     */
   @JsonProperty("ADDRESS")
    private String adress;

服務(wù)端接收如下:

    @PostMapping("/postPersonByJson")
    public Person postPersonByJson(@RequestBody Person person) {
        log.info("person:{}", person);
        return person;
    }

HttpEntity

HttpEntity 和@RequestBody使用相同,多了一個header

### test httpEntity
POST http://localhost:8080/api/person/accounts
Content-Type: application/json
{
  "id": 1,
  "name": "tom",
  "age": 12,
  "ADDRESS": "beijing"
}
    /**
     * HttpEntity 和@RequestBody使用相同,多了一個header
     *
     * @param entity
     */
    @PostMapping("/accounts")
    public void handle(HttpEntity<Person> entity) {
        log.info("請求頭部為:{},請求體為:{}", entity.getHeaders(), entity.getBody());
    }

@RequestHeader

@RequestHeader 主要用于接收前端header中的參數(shù),可以獲取指定的header屬性,也可以通過Map統(tǒng)一接收請求頭中的參數(shù)

### test header
GET http://localhost:8080/api/person/getPersonHeader
Accept-Encoding: deflate
Keep-Alive: 11
### test headerByMap
GET http://localhost:8080/api/person/getPersonHeaderByMap
Accept-Encoding: deflate
Keep-Alive: 11
    /**
     * 接收header參數(shù)
     */
    @GetMapping("/getPersonHeader")
    public void getPersonHeader(@RequestHeader("Accept-Encoding") String encoding,
                                @RequestHeader("Keep-Alive") long keepAlive) {
        log.info("Accept-Encoding:{},Keep-Alive:{}", encoding, keepAlive);
    }
    /**
     * map接收header參數(shù)
     * 使用map接收key值會變成小寫
     */
    @GetMapping("/getPersonHeaderByMap")
    public void getPersonHeader(@RequestHeader Map<String, String> param) {
        param.forEach((key, value) -> {
            log.info("key:{},value:{}", key, value);
        });
        log.info("Accept-Encoding:{},Keep-Alive:{}", param.get("accept-encoding"), param.get("keep-alive"));
    }

@PathVariable

使用@PathVariable注解可以獲取url中的參數(shù),也可以使用正則表達式提取url中的多個變量

### test PathVariable
GET http://localhost:8080/api/person/1
### test PathVariable  regular expression
GET http://localhost:8080/api/person/express/spring-web-3.0.5.jar
    /**
     * 獲取url中的參數(shù)
     */
    @GetMapping("/{id}")
    public void getPersonById(@PathVariable String id) {
        log.info("id:{}", id);
    }
    /**
     * 通過正則表達式獲取url中name,version,ext等信息
     */
    @GetMapping("/express/{name:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{ext:\\.[a-z]+}")
    public void handle(@PathVariable String name, @PathVariable String version, @PathVariable String ext) {
        log.info("name:{},version:{},ext:{}", name, version, ext);
    }

@ModelAttribute

@ModelAttribute可以作用在方法上和方法參數(shù)上,

標記在方法的參數(shù)上,會將客戶端傳遞過來的參數(shù)按名稱注入到指定對象中

標記在方法上,會在每一個@RequestMapping標注的方法前執(zhí)行。

###test modelAttribute
POST http://localhost:8080/api/person/postPersonByModel/123
Content-Type: application/x-www-form-urlencoded
name=tom&age=14&address=北京
###test getModelAttribute
GET http://localhost:8080/api/person/getPersonByModel/456?name=tom&age=14&address=北京
    @PostMapping("/postPersonByModel/{id}")
    public void postPersonByModel(@ModelAttribute Person person) {
        log.info("person:{}", person);
    }
    @GetMapping("/getPersonByModel/{id}")
    public void getPersonByModel(@ModelAttribute Person person) {
        log.info("person:{}", person);
    }

或者將@ModelAttribute注解放到方法上,如果有公共參數(shù)需要進行處理,可以使用@ModelAttribute進行前置操作例如:

### testModelAttribute
POST http://localhost:8080/api/person/testModelAttribute
Content-Type: application/x-www-form-urlencoded
token: 123
name=tom&age=14&address=北京
    @ModelAttribute
    public void userInfo(ModelMap modelMap, HttpServletRequest request) {
        log.info("執(zhí)行modelAttribute方法");
        //模擬通過token獲取userId的過程
        String id = request.getHeader("token").toString();
        modelMap.addAttribute("userId", id);
    }
    @PostMapping("/testModelAttribute")
    public void testModelAttribute(@ModelAttribute("userId") String userId, Person person) {
        log.info("userId:{}", userId);
    }

到此這篇關(guān)于SpringBoot如何優(yōu)雅接收前端請求參數(shù)的文章就介紹到這了,更多相關(guān)SpringBoot接收前端請求參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用IDEA反編譯沒有擦除泛型的原因解析

    使用IDEA反編譯沒有擦除泛型的原因解析

    Java泛型引入至今已有近20年,“偽泛型”已被認為是所有開發(fā)者的共識,沒有必要再在反編譯后體現(xiàn)出來反倒大大降低了可讀性,這篇文章主要介紹了使用IDEA反編譯沒有擦除泛型的原因解析,需要的朋友可以參考下
    2023-05-05
  • Java volatile關(guān)鍵字特性講解上篇

    Java volatile關(guān)鍵字特性講解上篇

    JMM要求保證可見性、原子性、有序性,volatile可以保證其中的兩個,本篇文章具體驗證volatile的可見性,不原子性和禁重排,同時解決volatile的不保證原子性,讓代碼具有原子性
    2022-12-12
  • java代碼實現(xiàn)MD5加密及驗證過程詳解

    java代碼實現(xiàn)MD5加密及驗證過程詳解

    這篇文章主要介紹了java代碼實現(xiàn)MD5加密及驗證過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • 詳解Java如何實現(xiàn)防止惡意注冊

    詳解Java如何實現(xiàn)防止惡意注冊

    惡意注冊通常是指使用自動化腳本或者機器人在短時間內(nèi)進行大量的注冊行為,這種行為會對系統(tǒng)造成壓力,甚至?xí)?dǎo)致系統(tǒng)癱瘓。所以本文為大家總結(jié)了一些防止惡意注冊的方法,需要的可以參考一下
    2023-04-04
  • Springboot 整合 Java DL4J 實現(xiàn)文物保護系統(tǒng)的詳細過程

    Springboot 整合 Java DL4J 實現(xiàn)文物保護系統(tǒng)的詳細過程

    在數(shù)字化時代,文物保護尤為關(guān)鍵,本文介紹如何利用SpringBoot和Deeplearning4j構(gòu)建一個圖像識別的文物保護系統(tǒng),系統(tǒng)采用卷積神經(jīng)網(wǎng)絡(luò)(CNN),能夠識別文物的損壞情況,本文介紹Springboot 整合 Java DL4J 實現(xiàn)文物保護系統(tǒng),感興趣的朋友一起看看吧
    2024-10-10
  • Java實現(xiàn)鼠標模擬與鍵盤映射

    Java實現(xiàn)鼠標模擬與鍵盤映射

    這篇文章主要為大家詳細介紹了Java實現(xiàn)鼠標模擬與鍵盤映射,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • 通過FeignClient如何獲取文件流steam?is?close問題

    通過FeignClient如何獲取文件流steam?is?close問題

    這篇文章主要介紹了通過FeignClient如何獲取文件流steam?is?close問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Spring Cloud分布式定時器之ShedLock的實現(xiàn)

    Spring Cloud分布式定時器之ShedLock的實現(xiàn)

    這篇文章主要介紹了Spring Cloud分布式定時器之ShedLock的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 使用Netty搭建服務(wù)端和客戶端過程詳解

    使用Netty搭建服務(wù)端和客戶端過程詳解

    這篇文章主要介紹了使用Netty搭建服務(wù)端和客戶端過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • Java中List集合去重方法以及效率對比

    Java中List集合去重方法以及效率對比

    這篇文章主要給大家介紹了關(guān)于Java中List集合去重方法以及效率對比的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04

最新評論

丹寨县| 怀来县| 纳雍县| 长宁区| 万源市| 胶南市| 深圳市| 井冈山市| 东丰县| 株洲县| 陵川县| 西乌珠穆沁旗| 宁安市| 康乐县| 阳城县| 嘉兴市| 民县| 内乡县| 库尔勒市| 赤峰市| 舞阳县| 电白县| 革吉县| 呈贡县| 建始县| 卢氏县| 莎车县| 白玉县| 七台河市| 乌什县| 瓮安县| 天气| 澄江县| 顺平县| 威海市| 阜南县| 绥化市| 弋阳县| 行唐县| 奇台县| 宜州市|