SpringBoot接受參數(shù)相關(guān)注解方式
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)文章
springcloud本地服務(wù)不注冊到注冊中心的解決方案
這篇文章主要介紹了springcloud本地服務(wù)不注冊到注冊中心,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07java中兩個字符串的拼接、整數(shù)相加和浮點數(shù)相加實現(xiàn)代碼
這篇文章主要為大家介紹java中從鍵盤讀取用戶輸入兩個字符串,實現(xiàn)這兩個字符串的拼接、整數(shù)相加和浮點數(shù)相加,并輸出結(jié)果,需要的朋友可以參考下2021-05-05Java開發(fā)之內(nèi)部類對象的創(chuàng)建及hook機制分析
這篇文章主要介紹了Java開發(fā)之內(nèi)部類對象的創(chuàng)建及hook機制,結(jié)合實例形式分析了java基于hook機制內(nèi)部類對象的創(chuàng)建與使用,需要的朋友可以參考下2018-01-01SpringBoot實現(xiàn)Excel文件批量上傳導(dǎo)入數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了SpringBoot實現(xiàn)Excel文件批量上傳導(dǎo)入數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11