Springboot獲取請求引數
阿新 • • 發佈:2019-01-31
參考:
慕課網 廖師兄:兩小時學會Springboot
第一種方式:
假如http://localhost:8080/hello為請求,springboot為需要傳遞的引數
http://localhost:8080/hello/spingboot
獲取此種請求的引數的方式,使用@PathVariable註解
1.HelloController
package com.example; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello/{params}")//獲取請求為http://localhost:8080/hello/XXX 型別的引數 public String hello(@PathVariable("params") String paramsStr) {//宣告一個變數接收請求中的引數 return "parameter is "+paramsStr; } }
2.執行程式,輸入http://localhost:8080/hello/spingboot進行測試
第二種方式:
獲取請求為http://localhost:8080/hello?params=spingboot型別的引數,使用@RequesParam註解,使用方法為@RequesParam("請求中的引數名params"),
1.HelloController
package com.example; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { // @RequestMapping("/hello/{params}")//獲取請求為http://localhost:8080/hello/XXX型別的引數 // public String hello(@PathVariable("params") String paramsStr) {//宣告一個變數接收請求中的引數 // // return "parameter is "+paramsStr; // } //獲取請求為http://localhost:8080/hello?xxx=xxx型別的引數 @RequestMapping("/hello") public String hello(@RequestParam("params") String paramsStr) {//requestParam中的引數名稱與請求中引數名稱要一致 return "parameter is "+paramsStr; } }
2.啟動程式,輸入http://localhost:8080/hello?params=spingboot