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

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

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

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.細節(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.細節(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.細節(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.細節(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集合之Set、HashSet、LinkedHashSet和TreeSet深度解析

    Java集合之Set、HashSet、LinkedHashSet和TreeSet深度解析

    這篇文章主要介紹了Java集合之Set、HashSet、LinkedHashSet和TreeSet深度解析,List是有序集合的根接口,Set是無序集合的根接口,無序也就意味著元素不重復(fù),更嚴(yán)格地說,Set集合不包含一對元素e1和e2 ,使得e1.equals(e2) ,并且最多一個空元素,需要的朋友可以參考下
    2023-09-09
  • selenium-java實現(xiàn)自動登錄跳轉(zhuǎn)頁面方式

    selenium-java實現(xiàn)自動登錄跳轉(zhuǎn)頁面方式

    利用Selenium和Java語言可以編寫一個腳本自動刷新網(wǎng)頁,首先,需要確保Google瀏覽器和Chrome-Driver驅(qū)動的版本一致,通過指定網(wǎng)站下載對應(yīng)版本的瀏覽器和驅(qū)動,在Maven項目中添加依賴,編寫腳本實現(xiàn)網(wǎng)頁的自動刷新,此方法適用于需要頻繁刷新網(wǎng)頁的場景,簡化了操作,提高了效率
    2024-11-11
  • Java異常 Exception類及其子類(實例講解)

    Java異常 Exception類及其子類(實例講解)

    下面小編就為大家?guī)硪黄狫ava異常 Exception類及其子類(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • java中如何獲取相關(guān)參數(shù)

    java中如何獲取相關(guān)參數(shù)

    這篇文章主要介紹了java獲取系統(tǒng)屬性相關(guān)參數(shù)的方法,,需要的朋友可以參考下
    2015-07-07
  • spring redis 如何實現(xiàn)模糊查找key

    spring redis 如何實現(xiàn)模糊查找key

    這篇文章主要介紹了spring redis 如何實現(xiàn)模糊查找key的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java_Spring之XML?的?AOP?配置

    Java_Spring之XML?的?AOP?配置

    這篇文章主要介紹了Java_Spring中基于XML的AOP配置,上篇講到的是基于注解的AOP配置,對XML感興趣的同學(xué)可以參考閱讀本文
    2023-04-04
  • Spring Boot 整合mybatis 使用多數(shù)據(jù)源的實現(xiàn)方法

    Spring Boot 整合mybatis 使用多數(shù)據(jù)源的實現(xiàn)方法

    這篇文章主要介紹了Spring Boot 整合mybatis 使用多數(shù)據(jù)源的實現(xiàn)方法,需要的朋友可以參考下
    2018-03-03
  • @DS注解的使用,動態(tài)數(shù)據(jù)源,事務(wù)詳解

    @DS注解的使用,動態(tài)數(shù)據(jù)源,事務(wù)詳解

    在項目中使用多數(shù)據(jù)源時,可以借助苞米豆的dynamic-datasource-spring-boot-starter進行配置,首先需引入相應(yīng)的jar包,并在application.yml中設(shè)置主從數(shù)據(jù)源,其中一般選擇master作為默認(rèn)數(shù)據(jù)源,在實現(xiàn)類中通過@DS注解指定數(shù)據(jù)源
    2024-09-09
  • Java時間類Date類和Calendar類的使用詳解

    Java時間類Date類和Calendar類的使用詳解

    這篇文章主要介紹了Java時間類Date類和Calendar類的使用詳解,需要的朋友可以參考下
    2017-08-08
  • 基于Class.forName()用法及說明

    基于Class.forName()用法及說明

    這篇文章主要介紹了基于Class.forName()用法及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11

最新評論

肇源县| 即墨市| 平度市| 平阳县| 九龙城区| 泸水县| 阳山县| 赣榆县| 敦煌市| 长垣县| 台北市| 广平县| 凉城县| 新竹市| 南木林县| 巴彦淖尔市| 龙门县| 科技| 嫩江县| 喀喇沁旗| 临城县| 蓬溪县| 台中县| 宁明县| 平昌县| 霍城县| 宝坻区| 军事| 衡东县| 双牌县| 谷城县| 山东省| 延边| 成武县| 厦门市| 临夏市| 万年县| 五大连池市| 扬中市| 六盘水市| 长垣县|