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

SpringBoot接受參數(shù)相關(guān)注解方式

 更新時間:2024年12月09日 11:07:14   作者:S-X-S  
SpringBoot接受參數(shù)的注解包括@PathVariable、@RequestHeader、@RequestParameter、@CookieValue、@RequestBody、@RequestAttribute和@SessionAttribute等,每個注解都有詳細(xì)的使用方法和示例代碼

SpringBoot接受參數(shù)相關(guān)注解

1.基本介紹

2.@PathVariable 路徑參數(shù)獲取信息

1.代碼實例

1.index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>基本注解</h1>
<hr/>
<a href="/monster/100/king" rel="external nofollow" >@PathVariable-路徑變量:/monster/100/king</a>
</body>
</html>

2.ParameterController.java

package com.sun.springboot.controller;

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

import java.util.Map;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@RestController
public class ParameterController {

    @GetMapping("/monster/{id}/{name}") //接受兩個路徑參數(shù)
    public String pathVariable(@PathVariable("id") Integer id, @PathVariable("name") String name,
                               @PathVariable Map<String, String> map) { //這里的map指將所有的路徑參數(shù)都放到map中
        System.out.println("id:" + id + " name:" + name);
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key:" + entry.getKey() + " value: " + entry.getValue());
        }
        return "success"; //返回json給瀏覽器
    }

}

3.測試

2.細(xì)節(jié)說明

  • @PathVariable(“xxx”)必須跟{xxx}相對應(yīng)
  • 可以將所有的路徑參數(shù)放到map中 @PathVariable Map<String, String> map

3.@RequestHeader 請求頭獲取信息

1.代碼實例

1.index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>基本注解</h1>
<hr/>
<a href="/requestHeader" rel="external nofollow" >@RequestHeader-獲取請求頭信息</a>
</body>
</html>

2.ParameterController.java

package com.sun.springboot.controller;

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

import java.util.Map;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@RestController
public class ParameterController {

    @GetMapping("/requestHeader") //獲取請求頭的信息
    public String requestHeader(@RequestHeader("host") String host, @RequestHeader Map<String, String> header) {
        System.out.println("host:" + host);
        System.out.println(header);
        return "success";
    }

}

3.測試

2.細(xì)節(jié)說明

  • 請求頭的信息都是以key - value的形式存儲的
  • 可以通過@RequestHeader(“xxx”)來獲取xxx對應(yīng)的value
  • 也可以通過@RequestHeader Map<String, String> header將所有的key - value都封裝到map中

4.@RequestParameter 請求獲取參數(shù)信息

1.代碼實例

1.index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>基本注解</h1>
<hr/>
<a href="/hi?hobby=打籃球&hobby=踢球" rel="external nofollow" >@RequestParam-請求參數(shù)</a>
</body>
</html>

2.ParameterController.java

package com.sun.springboot.controller;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@RestController
public class ParameterController {

    @GetMapping("/hi")
    public String hi(@RequestParam(value = "name", defaultValue = "孫顯圣") String name,
                     @RequestParam("hobby") List<String> list) {
        System.out.println("name:" + name);
        System.out.println(list);
        return "success";
    }


}

3.測試

2.細(xì)節(jié)說明

  • 請求參數(shù)是可以設(shè)置默認(rèn)值的,使用defaultValue屬性即可
  • 請求參數(shù)還可以將同名的結(jié)果封裝到List中
  • 請求參數(shù)也可以使用@RequestParameter Map<String, String> map 將所有參數(shù)封裝到map中,但是如果有同名的結(jié)果只會得到第一個,因為map的key是唯一的

5.@CookieValue cookie獲取值

1.代碼實例

1.index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>基本注解</h1>
<hr/>
<a href="/cookie" rel="external nofollow" >@CookieValue-獲取cookie的值</a>
</body>
</html>

2.ParameterController.java

package com.sun.springboot.controller;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@RestController
public class ParameterController {

    @GetMapping("/cookie")
    //這里可以設(shè)置required = false意為不是必須存在的,如果不存在則得到的值就為null
    //如果后面的參數(shù)類型是Cookie,則會獲取Cookie對象并封裝到變量中
    public String cookie(@CookieValue(value = "cookie_key", required = false) String cookie_value,
                         @CookieValue(value = "username" , required = false) Cookie cookie, HttpServletRequest request) {
        //使用原生api獲取cookies
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie1 : cookies) {
            System.out.println(cookie1);
        }

        System.out.println(cookie_value);
        System.out.println("name:" + cookie.getName() + " value: " + cookie.getValue());

        return "success";
    }


}

3.測試

2.細(xì)節(jié)說明

  • @CookieValue可以根據(jù)后面要封裝的參數(shù)的類型來獲取指定的值,如果后面的類型是Cookie類型則會獲取一個Cookie對象并封裝進入,如果是String類型則會獲取Cookie的value來進行封裝
  • 還可以通過Servlet原生api的request來獲取所有的cookie
  • @CookieValue中有屬性required默認(rèn)為true,意為必須存在,否則報錯,如果設(shè)置為false,則如果獲取不到則為null

6.@RequestBody 處理json請求,post請求體獲取信息

1.代碼實例

1.index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>基本注解</h1>
<hr/>
<form action="/requestBody" method="post">
    <input type="text" name="username"><br>
    <input type="text" name="password"><br>
    <input type="submit" value="submit">
</form>
</body>
</html>

2.ParameterController.java

package com.sun.springboot.controller;

import org.springframework.web.bind.annotation.*;



/**
 * @author 孫顯圣
 * @version 1.0
 */
@RestController
public class ParameterController {

    @PostMapping("requestBody")
    public String getRequestBody(@RequestBody String requestBody) { //獲取請求體
        System.out.println(requestBody);
        return "success";
    }

}

3.測試

7.@RequestAttribute 請求域獲取信息

1.代碼實例

1.RequestController.java

package com.sun.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
/**
 * @author 孫顯圣
 * @version 1.0
 */
@Controller
public class RequestController {

    @GetMapping("/login")
    public String login(HttpServletRequest request) {
        //在Request域中存放一些信息
        request.setAttribute("name", "sun");
        request.setAttribute("age", 13);
        //調(diào)用視圖解析器,請求轉(zhuǎn)發(fā)到/ok
        return "forward:/ok";
    }

    @ResponseBody
    @GetMapping("/ok")
    public String ok(@RequestAttribute(value = "name", required = false) String name) { //使用注解來獲取請求域中的信息并封裝到參數(shù)中
        System.out.println("name: " + name);
        return "success"; //返回json給瀏覽器
    }
}

2.配置視圖解析器 application.yml

spring:
  mvc:
    view: #配置了視圖解析器
      suffix: .html #后綴
      prefix: / #前綴,指的是根目錄

3.測試

8.@SessionAttribute session域獲取信息

1.代碼實例

1.SessionController.java

    package com.sun.springboot.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.SessionAttribute;
    
    import javax.servlet.http.HttpServletRequest;
    /**
     * @author 孫顯圣
     * @version 1.0
     */
    @Controller
    public class SessionController {
    
        @GetMapping("/login")
        public String login(HttpServletRequest request) {
            //在session域中設(shè)置信息
            request.getSession().setAttribute("session", "session_value");
    
            //調(diào)用視圖解析器,請求轉(zhuǎn)發(fā)到/ok
            return "forward:/ok";
        }
    
        @ResponseBody
        @GetMapping("/ok")
        public String ok(@SessionAttribute(value = "session") String value) { //使用注解來獲取session域中的信息并封裝到參數(shù)中
            System.out.println("session: " + value);
            return "success"; //返回json給瀏覽器
        }
    }

2.配置視圖解析器(同上)

3.測試

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

1.代碼實例

1.RequestController.java

package com.sun.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@Controller
public class RequestController {

    @GetMapping("/login")
    public String login(Map<String, Object> map, Model model, HttpServletResponse response) {
        //給map封裝信息
        map.put("user", "sun");
        map.put("job", "工程師");

        //model封裝信息
        model.addAttribute("sal", 1000);

        //結(jié)果最后都會封裝到request域中

        //調(diào)用視圖解析器,請求轉(zhuǎn)發(fā)到/ok
        return "forward:/ok";
    }

    @ResponseBody
    @GetMapping("/ok")
    public String ok(@RequestAttribute("user") String user, @RequestAttribute("job") String job,
                     @RequestAttribute("sal") Integer sal) { //使用注解來獲取請求域中的信息并封裝到參數(shù)中
        System.out.println("user:" + user + " job:" + job + " sal:" +sal);
        return "success"; //返回json給瀏覽器
    }
}

2.測試

2.HttpServletResponse給瀏覽器設(shè)置cookie

1.代碼實例

package com.sun.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@Controller
public class RequestController {

    @GetMapping("/login")
    public String login(HttpServletResponse response) {
        Cookie cookie = new Cookie("cookie_name", "cookie_value");
        response.addCookie(cookie);

        //調(diào)用視圖解析器,重定向到/ok,不能使用請求轉(zhuǎn)發(fā),因為雖然響應(yīng)給客戶端cookie了,
        // 但是由于是請求轉(zhuǎn)發(fā),第二個controller得到的是最開始的請求,那時候還沒有cookie
        return "redirect:/ok";
    }

    @ResponseBody
    @GetMapping("/ok")
    public String ok(@CookieValue("cookie_name") Cookie cookie) {
        //獲取cookie
        System.out.println("key:" + cookie.getName() + " value:" + cookie.getValue());
        return "success"; //返回json給瀏覽器
    }
}

2.測試

總結(jié)

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

相關(guān)文章

  • Java中二維數(shù)組的正確使用方法介紹

    Java中二維數(shù)組的正確使用方法介紹

    Java中有一維數(shù)組,二維數(shù)組以及多維數(shù)組,在這篇文章中,將給大家詳細(xì)介紹一下如何正確使用Java中的二維數(shù)組,感興趣的小伙伴跟著小編一起學(xué)習(xí)吧
    2023-05-05
  • Java Web三層架構(gòu)的配置詳解

    Java Web三層架構(gòu)的配置詳解

    這篇文章主要介紹了Java Web三層架構(gòu)的配置方法,需要的朋友可以參考下
    2014-10-10
  • springcloud本地服務(wù)不注冊到注冊中心的解決方案

    springcloud本地服務(wù)不注冊到注冊中心的解決方案

    這篇文章主要介紹了springcloud本地服務(wù)不注冊到注冊中心,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • SpringBoot初始化加載配置的八種方式總結(jié)

    SpringBoot初始化加載配置的八種方式總結(jié)

    在日常開發(fā)時,我們常常需要 在SpringBoot應(yīng)用啟動時執(zhí)行某一段邏輯,如獲取一些當(dāng)前環(huán)境的配置或變量、向數(shù)據(jù)庫寫入一些初始數(shù)據(jù)或者連接某些第三方系統(tǒng),確認(rèn)對方可以工作,那么在實現(xiàn)初始化邏輯代碼時就需要小心了,所以本文介紹了SpringBoot初始化加載配置的方式
    2024-12-12
  • 詳解RestTemplate?用法

    詳解RestTemplate?用法

    RestTemplate?是從?Spring3.0?開始支持的一個?HTTP?請求工具,也有的稱之為網(wǎng)絡(luò)框架,說白了就是Java版本的一個postman,這篇文章主要介紹了詳解RestTemplate?用法,需要的朋友可以參考下
    2022-07-07
  • java中兩個字符串的拼接、整數(shù)相加和浮點數(shù)相加實現(xiàn)代碼

    java中兩個字符串的拼接、整數(shù)相加和浮點數(shù)相加實現(xiàn)代碼

    這篇文章主要為大家介紹java中從鍵盤讀取用戶輸入兩個字符串,實現(xiàn)這兩個字符串的拼接、整數(shù)相加和浮點數(shù)相加,并輸出結(jié)果,需要的朋友可以參考下
    2021-05-05
  • Java之Maven工程打包jar

    Java之Maven工程打包jar

    Maven打包一般可以生成兩種包一種是可以直接運行的包,一種是依賴包(只是編譯包)。Maven默認(rèn)打包時jar,如果需要修改其他類型,可以修改pom.xml。感興趣的同學(xué)可以參考閱讀
    2023-04-04
  • Java開發(fā)之內(nèi)部類對象的創(chuàng)建及hook機制分析

    Java開發(fā)之內(nèi)部類對象的創(chuàng)建及hook機制分析

    這篇文章主要介紹了Java開發(fā)之內(nèi)部類對象的創(chuàng)建及hook機制,結(jié)合實例形式分析了java基于hook機制內(nèi)部類對象的創(chuàng)建與使用,需要的朋友可以參考下
    2018-01-01
  • 你都理解創(chuàng)建線程池的參數(shù)嗎?

    你都理解創(chuàng)建線程池的參數(shù)嗎?

    這篇文章主要介紹了創(chuàng)建線程池參數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • SpringBoot實現(xiàn)Excel文件批量上傳導(dǎo)入數(shù)據(jù)庫

    SpringBoot實現(xiàn)Excel文件批量上傳導(dǎo)入數(shù)據(jù)庫

    這篇文章主要為大家詳細(xì)介紹了SpringBoot實現(xiàn)Excel文件批量上傳導(dǎo)入數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11

最新評論