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

Springboot接收Get參數(shù)實(shí)踐過(guò)程

 更新時(shí)間:2024年12月09日 16:19:35   作者:梅塢茶坊  
本文主要介紹了在Spring Boot中如何接收不同類型的請(qǐng)求參數(shù),包括在路徑中直接傳遞參數(shù)、跟在問(wèn)號(hào)后面?zhèn)鬟f參數(shù)、使用Map接收參數(shù)、接收數(shù)組以及使用對(duì)象接收參數(shù)等方法

一、參數(shù)直接在路徑中

1.假設(shè)請(qǐng)求地址是如下這種 RESTful 風(fēng)格

hangge 這個(gè)參數(shù)值直接放在路徑里面:

http://localhost:8080/helloworld/張三

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

    @GetMapping("/helloworld/{name}")
    public String helloworld(@PathVariable("name") String name) {
        return "獲取到的name是:" + name;
    }


}

二、參數(shù)跟在 ? 號(hào)后面

1.獲取參數(shù)的基本方法

(1)假設(shè)請(qǐng)求地址是如下這種傳統(tǒng)方式,參數(shù)跟在問(wèn)號(hào)后面:

http://localhost:8080/helloworld1?name=張三

(2)Controller 可以這么獲取該參數(shù):

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
 
@RestController
public class HelloController {
    @GetMapping("/helloworld1")
    public String helloworld1(@RequestParam("name") String name) {
        return "獲取到的name是:" + name;
    }
}

2.參數(shù)沒有傳遞的情況

(1)如果沒有傳遞參數(shù) Controller 將會(huì)報(bào)錯(cuò),我們可以使用 required = false 標(biāo)注參數(shù)是非必須的。

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
 
@RestController
public class HelloController {
    @GetMapping("/helloworld2")
    public String helloworld2(@RequestParam(name = "name", required = false) String name) {
        return "獲取到的name是:" + name;
    }
}

(2)或者可以指定個(gè)默認(rèn)值,當(dāng)沒有傳遞參數(shù)時(shí)自動(dòng)使用默認(rèn)值:

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
 
@RestController
public class HelloController {
    @GetMapping("/helloworld3")
    public String helloworld3(@RequestParam(name = "name", defaultValue = "xxx") String name) {
        return "獲取到的name是:" + name;
    }
}

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

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

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

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

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

(1)假設(shè)請(qǐng)求地址是如下這種,有多個(gè)同名參數(shù):

http://localhost:8080/helloworld5?name=zhangsan&name=lisi

(2)我們可以定義一個(gè)數(shù)組類型的參數(shù)來(lái)接收:

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
 
@RestController
public class HelloController {
    @GetMapping("/helloworld5")
    public String helloworld5(@RequestParam("name") String[] names) {
        String result = "";
        for(String name:names){
            result += name + "<br>";
        }
        return result;
    }
}

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

1.基本用法

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

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
 
@RestController
public class HelloController {
    @GetMapping("/helloworld6")
    public String helloworld6(User user) {
        return "name:" + user.getName() + "<br> age:" + user.getAge();
    }
}

(2)User 類的定義如下,到時(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;
    }
 
    public void setAge(Integer age) {
        this.age = age;
    }
}

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

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

2.指定參數(shù)前綴

(1)如果傳遞的參數(shù)有前綴,且前綴與接收實(shí)體類的名稱不同相,那么參數(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 {
    @GetMapping("/helloworld7")
    public String helloworld7(@ModelAttribute("u") User user) {
        return "name:" + user.getName() + "<br> age:" + user.getAge();
    }
 
    @InitBinder("u")
    private void initBinder(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("u.");
    }
}

(3)重啟程序可以看到參數(shù)以及成功接收了:

3.構(gòu)造多個(gè)對(duì)象來(lái)接收參數(shù)

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

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
 
@RestController
public class HelloController {
    @GetMapping("/helloworld8")
    public String helloworld8(User user, Phone phone) {
        return "name:" + user.getName() + "<br> age:" + user.getAge()
                + "<br> number:" + phone.getNumber();
    }
}

(2)新增的 Phone 類定義如下:

public class Phone {
    private String number;
 
    public String getNumber() {
        return number;
    }
 
    public void setNumber(String number) {
        this.number = number;
    }
}

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

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java仿windows記事本功能完整版

    java仿windows記事本功能完整版

    這篇文章主要為大家詳細(xì)介紹了java仿windows記事本功能完整版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • SpringBoot同一個(gè)方法操作多個(gè)數(shù)據(jù)源保證事務(wù)一致性

    SpringBoot同一個(gè)方法操作多個(gè)數(shù)據(jù)源保證事務(wù)一致性

    本文探討了在Spring Boot應(yīng)用中,如何在同一個(gè)方法中操作多個(gè)數(shù)據(jù)源并保證事務(wù)的一致性,由于聲明式事務(wù)的限制,直接使用@Transactional注解無(wú)法滿足需求,文章介紹了解決方案:編程式事務(wù),它允許在代碼級(jí)別更靈活地管理事務(wù),確保多數(shù)據(jù)源操作的事務(wù)一致性
    2024-11-11
  • MyBatis傳入多個(gè)參數(shù)時(shí)parameterType的寫法

    MyBatis傳入多個(gè)參數(shù)時(shí)parameterType的寫法

    這篇文章主要介紹了MyBatis傳入多個(gè)參數(shù)時(shí)parameterType的寫法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Spring Boot整合Redis的完整步驟

    Spring Boot整合Redis的完整步驟

    這篇文章主要給大家介紹了關(guān)于Spring Boot整合Redis的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • SpringBoot中處理跨域請(qǐng)求CORS的全面指南

    SpringBoot中處理跨域請(qǐng)求CORS的全面指南

    跨域資源共享是一種安全機(jī)制,它允許Web應(yīng)用程序在一個(gè)域上的資源請(qǐng)求另一個(gè)域上的資源,下面就跟隨小編一起來(lái)深入了解下SpringBoot中處理跨域請(qǐng)求CORS的具體操作吧
    2025-04-04
  • java實(shí)現(xiàn)6種字符串?dāng)?shù)組的排序(String array sort)

    java實(shí)現(xiàn)6種字符串?dāng)?shù)組的排序(String array sort)

    這篇文章主要介紹了java實(shí)現(xiàn)6種字符串?dāng)?shù)組的排序(String array sort),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • SpringBoot開發(fā)中的組件和容器詳解

    SpringBoot開發(fā)中的組件和容器詳解

    這篇文章主要介紹了SpringBoot開發(fā)中的組件和容器詳解,SpringBoot 提供了一個(gè)內(nèi)嵌的 Tomcat 容器作為默認(rèn)的 Web 容器,同時(shí)還支持其他 Web 容器和應(yīng)用服務(wù)器,需要的朋友可以參考下
    2023-09-09
  • 淺談一下Java線程組ThreadGroup

    淺談一下Java線程組ThreadGroup

    ThreadGroup是為了方便線程管理出現(xiàn)了,可以統(tǒng)一設(shè)定線程組的一些屬性,比如setDaemon,設(shè)置未處理異常的處理方法,設(shè)置統(tǒng)一的安全策略等等,需要的朋友可以參考下
    2023-05-05
  • 超詳細(xì)講解Java秒殺項(xiàng)目用戶驗(yàn)證模塊的實(shí)現(xiàn)

    超詳細(xì)講解Java秒殺項(xiàng)目用戶驗(yàn)證模塊的實(shí)現(xiàn)

    這是一個(gè)主要使用java開發(fā)的秒殺系統(tǒng),項(xiàng)目比較大,所以本篇只實(shí)現(xiàn)了用戶驗(yàn)證模塊,代碼非常詳盡,感興趣的朋友快來(lái)看看
    2022-03-03
  • Java連接各種數(shù)據(jù)庫(kù)的方法

    Java連接各種數(shù)據(jù)庫(kù)的方法

    這篇文章主要介紹了Java連接各種數(shù)據(jù)庫(kù)的方法,實(shí)例分析了java連接MySQL、SQL Server、Sysbase、Oracle、PostgreSQL及DB2等數(shù)據(jù)庫(kù)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-02-02

最新評(píng)論

濮阳县| 汪清县| 密山市| 龙胜| 博兴县| 都匀市| 娄烦县| 睢宁县| 怀宁县| 金门县| 大理市| 长治市| 任丘市| 揭西县| 阿坝县| 宜良县| 泰和县| 通江县| 佛学| 平武县| 常德市| 长治市| 泸西县| 临猗县| 慈溪市| 西藏| 镇安县| 凤冈县| 桃源县| 邮箱| 佛山市| 开化县| 泊头市| 漾濞| 上高县| 嫩江县| 龙里县| 十堰市| 滦平县| 富锦市| 新乡市|