SpringBoot请求参数常用注解
@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);
}
}