SpringBoot請求引數常用註解
阿新 • • 發佈:2021-10-07
@GetMapping("/car/{id}/owner/{username}") public Map<String, Object> getCar( //路徑變數 @PathVariable("id") Integer id, @PathVariable("username") String username, @PathVariable Map<String, String> pv, //獲取請求頭 @RequestHeader("User-Agent") String userAgent, @RequestHeader Map<String, String> headerMap, //獲取請求引數 @RequestParam("age") Integer age, @RequestParam("inters") List<String> inters, @RequestParam Map<String, String> paramMap, //獲取cookie值 @CookieValue("_ga") String _ga, @CookieValue("_ga") Cookie cookie) { HashMap<String, Object> map = new HashMap<>(); map.put("id", id); map.put("username", username); map.put("pv", pv); map.put("userAgent", userAgent); map.put("headerMap", headerMap); map.put("age", age); map.put("inters", inters); map.put("paramMap", paramMap); map.put("_gz", _ga); map.put("cookie", cookie); return map; } @PostMapping("/save") public Map postMethod( //獲取請求體[POST] @RequestBody String content ) { System.out.println(content); HashMap<String, Object> map = new HashMap<>(); map.put("content", content); return map; }
@GetMapping("/goto") public String goToPage(HttpServletRequest request) { request.setAttribute("msg", "成功了。。。"); request.setAttribute("code", 200); return "forward:/success"; } @ResponseBody @GetMapping("/success") public Map success(@RequestAttribute("msg") String msg, @RequestAttribute("code") Integer code, HttpServletRequest request ) { HashMap<String, Object> map = new HashMap<>(); Object reqMethodMsg = request.getAttribute("msg"); Object reqMethodCode = request.getAttribute("code"); map.put("reqMethodMsg", reqMethodMsg); map.put("reqMethodCode", reqMethodCode); map.put("annotationMsg", msg); map.put("annotationCode", code); return map; }
矩陣路徑變數
@ResponseBody @GetMapping("/cars/{bossId}/{empId}") public Map carsSell(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge, @MatrixVariable(value = "age",pathVar = "empId") Integer empAge ) { HashMap<String, Object> map = new HashMap<>(); map.put("bossAge", bossAge); map.put("empAge", empAge); return map; }
矩陣路徑變數功能需手動開啟
@Configuration(proxyBeanMethods = false)
public class WebConfig implements WebMvcConfigurer {
// @Bean
// public WebMvcConfigurer webMvcConfigurer(){
// return new WebMvcConfigurer() {
// @Override
// public void configurePathMatch(PathMatchConfigurer configurer) {
// UrlPathHelper urlPathHelper = new UrlPathHelper();
// //設定不移除url分號後面的內容,矩陣變數功能就可以生效
// urlPathHelper.setRemoveSemicolonContent(false);
// configurer.setUrlPathHelper(urlPathHelper);
// }
// };
// }
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
//設定不移除url分號後面的內容,矩陣變數功能就可以生效
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}