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

SpringBoot接收參數(shù)使用的注解實(shí)例講解

 更新時(shí)間:2022年08月17日 16:45:50   作者:llp1110  
這篇文章主要介紹了詳解SpringBoot接收參數(shù)使用的幾種常用注解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.基本介紹

SpringBoot 接收客戶端提交數(shù)據(jù)/參數(shù)會(huì)使用到相關(guān)注解

詳 解 @PathVariable 、 @RequestHeader 、 @ModelAttribute 、 @RequestParam 、 @MatrixVariable、@CookieValue、@RequestBody

2.接收參數(shù)相關(guān)注解應(yīng)用實(shí)例

1.需求: 演示各種方式提交數(shù)據(jù)/參數(shù)給服務(wù)器,服務(wù)器如何使用注解接收

2.應(yīng)用實(shí)例演示

需求: 演示各種方式提交數(shù)據(jù)/參數(shù)給服務(wù)器,服務(wù)器如何使用注解接收

創(chuàng)建src\main\resources\static\index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
<h1>hello, llp</h1>
基本注解:
<hr/>
<a href="/monster/200/jack" rel="external nofollow" >@PathVariable-路徑變量 monster/200/jack</a><br/><br/>
</body>
</html>

@PathVariable 使用

演示@PathVariable 使用,創(chuàng)建src\main\java\com\llp\springboot\controller\ParameterController.java, 完成測(cè)試

@RestController
public class ParameterController {
    /**
     * /monster/{id}/{name} 解讀
     * 1. /monster/{id}/{name} 構(gòu)成完整請(qǐng)求路徑
     * 2. {id} {name} 就是占位變量
     * 3. @PathVariable("name"): 這里name 和{name} 命名保持一致
     * 4. String name_ 這里自定義,和{name}命名無(wú)關(guān)
     * 5. @PathVariable Map<String, String> map 把所有傳遞的值傳入map
     * 6. 可以看下@PathVariable源碼
     */
    @GetMapping("/monster/{id}/{name}")
    public String pathVariable(@PathVariable("id") Integer id,
                               @PathVariable("name") String name,
                               @PathVariable Map<String, String> map) {
        System.out.println("id-" + id);
        System.out.println("name-" + name);
        System.out.println("map-" + map);
        return "success";
    }
}

@RequestHeader 使用

演示@RequestHeader 使用,修改 ParameterController.java , 完成測(cè)試

√ 修改 index.html

<a href="/requestHeader" rel="external nofollow" >@RequestHeader-獲取Http請(qǐng)求頭 </a><br/><br/>

√ 修改 ParameterController.java

/**
 * @RequestHeader("Host") 獲取http請(qǐng)求頭的 host信息
 * @RequestHeader Map<String, String> header: 獲取到http請(qǐng)求的所有信息
 */
@GetMapping("/requestHeader")
public String requestHeader(@RequestHeader("host") String host,
                            @RequestHeader Map<String, String> header,
                            @RequestHeader("accept") String accept) {
    System.out.println("host-" + host);
    System.out.println("header-" + header);
    System.out.println("accept-" + accept);
    return "success";
}

@RequestParam 使用

演示@RequestParam 使用,修改 ParameterController.java , 完成測(cè)試

√ 修改 index.html

<a href="/hi?name=wukong&fruit=apple&fruit=pear&id=300&address=北京" rel="external nofollow" >@RequestParam-獲取請(qǐng)求參數(shù)</a><br/><br/>

√ 修改 ParameterController.java

/**
 * @param username wukong
 * @param fruits  List<String> fruits 接收集合 [apple, pear]
 * @param paras Map<String, String> paras 如果我們希望將所有的請(qǐng)求參數(shù)的值都獲取到,
 *              可以通過@RequestParam Map<String, String> paras這種方式
 *             一次性的接收所有的請(qǐng)求參數(shù) {name=wukong, fruit=apple, id=300, address=北京}
 *             如果接收的某個(gè)參數(shù)中有多個(gè)之值比如這里fruits是一個(gè)集合,從map中只能拿到一個(gè)
 *              可以理解map底層會(huì)將相同的key的value值進(jìn)行覆蓋
 * @return
 * @RequestParam
 */
@GetMapping("/hi")
public String hi(@RequestParam(value = "name") String username,
                 @RequestParam("fruit") List<String> fruits,
                 @RequestParam Map<String, String> paras) {
    //username-wukong
    System.out.println("username-" + username);
    //fruit-[apple, pear]
    System.out.println("fruit-" + fruits);
    //paras-{name=wukong, fruit=apple, id=300, address=北京}
    System.out.println("paras-" + paras);
    return "success";
}

@CookieValue 使用

演示@CookieValue 使用,修改 ParameterController.java , 完成測(cè)試

√ 修改 index.html

<a href="/cookie" rel="external nofollow" >@CookieValue-獲取cookie值</a><br/><br/>

√ 修改 ParameterController.java

/**
 * 因?yàn)槲业臑g覽器目前沒有cookie,我們可以自己設(shè)置cookie[技巧還是非常有用]
 * 如果要測(cè)試,可以先寫一個(gè)方法,在瀏覽器創(chuàng)建對(duì)應(yīng)的cookie
 * 說明 1. value = "cookie_key" 表示接收名字為 cookie_key的cookie
 * 2. 如果瀏覽器攜帶來對(duì)應(yīng)的cookie , 那么 后面的參數(shù)是String ,則接收到的是對(duì)應(yīng)對(duì)value
 * 3. 后面的參數(shù)是Cookie ,則接收到的是封裝好的對(duì)應(yīng)的cookie
 */
@GetMapping("/cookie")
public String cookie(@CookieValue(value = "cookie_key", required = false) String cookie_value,
                     HttpServletRequest request,
                     @CookieValue(value = "username", required = false) Cookie cookie) {
    System.out.println("cookie_value-" + cookie_value);
    if (cookie != null) {
        System.out.println("username-" + cookie.getName() + "-" + cookie.getValue());
    }
    System.out.println("-------------------------");
    Cookie[] cookies = request.getCookies();
    for (Cookie cookie1 : cookies) {
        System.out.println(cookie1.getName() + "=>" + cookie1.getValue());
    }
    return "success";
}

@RequestBody 使用

演示@RequestBody 使用,修改 ParameterController.java , 完成測(cè)試

√ 修改 index.html

<hr/>
<h1>測(cè)試@RequestBody獲取數(shù)據(jù): 獲取POST請(qǐng)求體</h1>
<form action="/save" method="post">
    姓名: <input name="name"/> <br>
    年齡: <input name="age"/> <br/>
    <input type="submit" value="提交"/>
</form>

√ 修改 ParameterController.java

/**
 * @RequestBody 是整體取出Post請(qǐng)求內(nèi)容
 */
@PostMapping("/save")
public String postMethod(@RequestBody String content) {
    System.out.println("content-" + content);
    return "success";
}

@RequestAttribute 和 @SessionAttribute使用

演示@RequestAttribute @SessionAttribute使用,創(chuàng)建 com/hspedu/web/controller/RequestController.java , 完成測(cè)試

√ 修改 index.html

<a href="/login" rel="external nofollow" >@RequestAttribute、@SessionAttribute-獲取request域、session屬性-</a>

√ 創(chuàng)建 RequestController.java

    @GetMapping("/login")
    public String login(HttpServletRequest request) {
        request.setAttribute("user", "llp");
        //向session中添加數(shù)據(jù)
        request.getSession().setAttribute("website", "http://www.baidu.com");
        //這里需要使用forward關(guān)鍵字,如果不適用則會(huì)走視圖解析器,這
        //里視圖解析器前綴配置的是/  后綴配置的.html  ---> /ok.html
        //而請(qǐng)求轉(zhuǎn)發(fā)在服務(wù)器端執(zhí)行,/被解析成 ip:port/工程路徑
        //進(jìn)而最終得到的完整路徑是 ip:port/工程路徑/ok.html
        //但是我們這里希望訪問的是 ip:port/工程路徑/ok這個(gè)請(qǐng)求路徑
        //因此這里手動(dòng)的設(shè)置forward:/ok ,底層會(huì)根據(jù)我們?cè)O(shè)置的路徑進(jìn)行請(qǐng)求轉(zhuǎn)發(fā)
        return "forward:/ok";
    }
    @GetMapping("ok")
    //返回字符串,不走視圖解析器
    @ResponseBody
    public String ok(@RequestAttribute(value = "user", required = false) String username,
                     @SessionAttribute(value = "website",required = false) String website, HttpServletRequest request) {
        System.out.println("username= " + username);
        System.out.println("通過servlet api 獲取 username-" +  request.getAttribute("user"));
        System.out.println("website = " + website);
        System.out.println("通過servlet api 獲取 website-"+request.getSession().getAttribute("website"));
        return "success";
    }
}

3.復(fù)雜參數(shù)

1.基本介紹

  • 在開發(fā)中,SpringBoot 在響應(yīng)客戶端請(qǐng)求時(shí),也支持復(fù)雜參數(shù)
  • Map、Model、Errors/BindingResult、RedirectAttributes、ServletResponse、SessionStatus、 UriComponentsBuilder、ServletUriComponentsBuilder、HttpSession
  • Map、Model 數(shù)據(jù)會(huì)被放在 request 域, 底層 request.setAttribute()
  • RedirectAttributes 重定向攜帶數(shù)據(jù)

2.復(fù)雜參數(shù)應(yīng)用實(shí)例

####1.說明 : 演示復(fù)雜參數(shù)的使用,重點(diǎn): Map、Model、ServletResponse

2.代碼實(shí)現(xiàn)

//響應(yīng)一個(gè)注冊(cè)請(qǐng)求
@GetMapping("/register")
public String register(Map<String,Object> map,
                       Model model,
                       HttpServletResponse response) {
    //如果一個(gè)注冊(cè)請(qǐng)求,會(huì)將注冊(cè)數(shù)據(jù)封裝到map或者model
    //map中的數(shù)據(jù)和model的數(shù)據(jù),會(huì)被放入到request域中
    map.put("user","llp");
    map.put("job","碼農(nóng)");
    model.addAttribute("sal", 2500);
    //一會(huì)我們?cè)贉y(cè)試response使用
    //我們演示創(chuàng)建cookie,并通過response 添加到瀏覽器/客戶端
    Cookie cookie = new Cookie("email", "123@sohu.com");
    response.addCookie(cookie);
    //請(qǐng)求轉(zhuǎn)發(fā)
    return "forward:/registerOk";
}
@ResponseBody
@GetMapping("/registerOk")
public String registerOk(HttpServletRequest request) {
    System.out.println("user-" + request.getAttribute("user"));
    System.out.println("job-" + request.getAttribute("job"));
    System.out.println("sal-" + request.getAttribute("sal"));
    return "success";
}

4.自定義對(duì)象參數(shù)-自動(dòng)封裝

1.基本介紹

  • 在開發(fā)中,SpringBoot 在響應(yīng)客戶端請(qǐng)求時(shí),也支持自定義對(duì)象參數(shù)
  • 完成自動(dòng)類型轉(zhuǎn)換與格式化
  • 支持級(jí)聯(lián)封裝

2.自定義對(duì)象參數(shù)-應(yīng)用實(shí)例

1.需求說明 : 演示自定義對(duì)象參數(shù)使用,完成自動(dòng)封裝,類型轉(zhuǎn)換

2.代碼實(shí)現(xiàn)

1.創(chuàng)建src\main\resources\static\save.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加妖怪</title></head>
<body><h1>添加妖怪-坐騎[測(cè)試封裝 POJO;]</h1>
<form action="/savemonster" method="post">
    編號(hào): <input name="id" value="100"><br/>
    姓名: <input name="name" value="牛魔王"/><br/>
    年齡: <input name="age" value="120"/> <br/>
    婚否: <input name="isMarried" value="true"/> <br/>
    生日: <input name="birth" value="2000/11/11"/> <br/>
    <!--注意這里car對(duì)象是monster的屬性,給對(duì)象屬性賦值時(shí)需要以對(duì)象名.字段名的方式-->
    坐騎:<input name="car.name" value="法拉利"/><br/>
    價(jià)格:<input name="car.price" value="99999.9"/>
    <input type="submit" value="保存"/>
</form>
</body>
</html>

2.修改src\main\java\com\llp\springboot\controller\ParameterController.java

@PostMapping("/savemonster")
public String saveMonster(Monster monster) {
    System.out.println("monster= " + monster);
    return "success";
}

到此這篇關(guān)于SpringBoot接收參數(shù)使用的注解實(shí)例講解的文章就介紹到這了,更多相關(guān)SpringBoot接收參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于java實(shí)現(xiàn)具有時(shí)效性文件鏈接

    基于java實(shí)現(xiàn)具有時(shí)效性文件鏈接

    這篇文章主要為大家詳細(xì)介紹了如何基于java實(shí)現(xiàn)具有時(shí)效性的文件鏈接,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下
    2023-12-12
  • Spring Native 基礎(chǔ)環(huán)境搭建過程

    Spring Native 基礎(chǔ)環(huán)境搭建過程

    Spring?Native可以通過GraalVM將Spring應(yīng)用程序編譯成原生鏡像,提供了一種新的方式來部署Spring應(yīng)用,本文介紹Spring?Native基礎(chǔ)環(huán)境搭建,感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • Java 隨機(jī)取字符串的工具類

    Java 隨機(jī)取字符串的工具類

    隨機(jī)數(shù)在實(shí)際中使用很廣泛,比如要隨即生成一個(gè)固定長(zhǎng)度的字符串、數(shù)字?;蛘唠S即生成一個(gè)不定長(zhǎng)度的數(shù)字、或者進(jìn)行一個(gè)模擬的隨機(jī)選擇等等。Java提供了最基本的工具,可以幫助開發(fā)者來實(shí)現(xiàn)這一切
    2014-01-01
  • 深入淺析TomCat Session管理分析

    深入淺析TomCat Session管理分析

    這篇文章主要介紹了深入淺析TomCat Session管理分析,需要的朋友可以參考下
    2015-11-11
  • 淺談Java后臺(tái)對(duì)JSON格式的處理操作

    淺談Java后臺(tái)對(duì)JSON格式的處理操作

    下面小編就為大家?guī)硪黄獪\談Java后臺(tái)對(duì)JSON格式的處理操作。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-06-06
  • 教你怎么在IDEA中創(chuàng)建java多模塊項(xiàng)目

    教你怎么在IDEA中創(chuàng)建java多模塊項(xiàng)目

    這篇文章主要介紹了教你怎么在IDEA中創(chuàng)建java多模塊項(xiàng)目,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • SpringBoot整合XxlJob分布式任務(wù)調(diào)度平臺(tái)

    SpringBoot整合XxlJob分布式任務(wù)調(diào)度平臺(tái)

    xxl-job是一個(gè)開源的分布式定時(shí)任務(wù)框架,它可以與其他微服務(wù)組件一起構(gòu)成微服務(wù)集群。它的調(diào)度中心(xxl-job)和執(zhí)行器(自己的springboot項(xiàng)目中有@XxlJob("定時(shí)任務(wù)名稱")的方法)是相互分離,分開部署的,兩者通過HTTP協(xié)議進(jìn)行通信
    2023-02-02
  • 設(shè)置Myeclipse中的代碼格式化、注釋模板及保存時(shí)自動(dòng)格式化

    設(shè)置Myeclipse中的代碼格式化、注釋模板及保存時(shí)自動(dòng)格式化

    這篇文章主要介紹了設(shè)置Myeclipse中的代碼格式化、注釋模板及保存時(shí)自動(dòng)格式化方法,需要的朋友可以參考下
    2014-10-10
  • 詳解Spring Cloud 熔斷機(jī)制--斷路器

    詳解Spring Cloud 熔斷機(jī)制--斷路器

    這篇文章主要介紹了詳解Spring Cloud 熔斷機(jī)制--斷路器,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • 深入理解SpringCloud之Eureka注冊(cè)過程分析

    深入理解SpringCloud之Eureka注冊(cè)過程分析

    eureka是一種去中心化的服務(wù)治理應(yīng)用,其顯著特點(diǎn)是既可以作為服務(wù)端又可以作為服務(wù)向自己配置的地址進(jìn)行注冊(cè),這篇文章主要介紹了深入理解SpringCloud之Eureka注冊(cè)過程分析
    2018-05-05

最新評(píng)論