@PathVariable、
可以获取path中我们选择自定义的地方的数据
@RequestHeader、
可以获取请求头中的数据
@RequestParam、
可以获取url的请求参数
@cookievalue、
用于获取cookie的值
@GetMapping("/anima/{id}/create/{name}") public MapgetAni(@PathVariable String id, @PathVariable String name, @PathVariable Map pv, @RequestHeader("User-Agent") String userAgent, @RequestHeader Map rh, @RequestParam("year") String year, @RequestParam("likes") Integer likes, @RequestParam Map rp, @cookievalue("Idea-216775ce") String Idea, @cookievalue("Idea-216775ce") cookie cookie){ Map mp = new HashMap<>(); mp.put("anima", id); mp.put("create", name); mp.put("pv",pv); mp.put("user-agent",userAgent); mp.put("RequestHeader", rh); mp.put("year",year); mp.put("likes", likes); mp.put("RequestParam", rp); mp.put("idea", Idea); mp.put("cookievalue", cookie); return mp; }
http://localhost:8888/anima/2021/create/化物语?year=2021&likes=22223
下面是返回的数据
@RequestBody
用于获取请求体
@PostMapping("/save") public MappostMethod(@RequestBody String content){ Map mp = new HashMap<>(); mp.put("content", content); return mp; }
访问的表单
提交
返回的是表单的数据的请求参数
@RequestAttribute
获取request域的属性
这里实现了一个转发的功能。
@GetMapping("/goto") public String goToPage(HttpServletRequest request){ request.setAttribute("msg","成功了。。。"); request.setAttribute("code","200"); return "forward:/success"; //转发到success请求 } @ResponseBody @GetMapping("/success") public Map success(@RequestAttribute("msg") String msg, @RequestAttribute("code") String code, HttpServletRequest request){ Mapmp = new HashMap<>(); Object msg1 = request.getAttribute("msg"); mp.put("msg1",msg1); mp.put("msg",msg); return mp; }
@MatrixVariable、
用来使用矩阵变量的方法,可以在当cookies禁用的时候使用url拼接jsessionid来获取数据。
需要注意的是该模式一般情况下是被禁用的
需要自己在容器里添加一个组件WebMvcConfigurer,并设置它的urlPathHelper的RemoveSemicolonContent为false。
@Bean public WebMvcConfigurer webMvcConfigurer(){ return new WebMvcConfigurer(){ @Override public void configurePathMatch(PathMatchConfigurer configurer){ UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); } }; }
下面是两种情况
// http://localhost:8888/cars/sell;low=20;brand=audi,byd @GetMapping("/cars/{path}") public MapcarsSell(@MatrixVariable("low") Integer low, @MatrixVariable("brand") List brand, @PathVariable("path") String path){ Map mp = new HashMap<>(); mp.put("low", low); mp.put("brand",brand); mp.put("path",path); return mp; } // 不同的path可以使用相同的value值 // http://localhost:8888/boss/2;name=zshh/5;name=koyomi @GetMapping("/boss/{bossId}/{empId}") public Map boss(@MatrixVariable(value = "name", pathVar = "bossId") String empName, @MatrixVariable(value = "name", pathVar = "empId") String bossName){ Map mp = new HashMap<>(); mp.put("empName",empName); mp.put("bossName",bossName); return mp; }
第一种情况,单个属性多个值。
第二种情况,在不同的path中设置请求参数
@ModelAttribute、
被@ModelAttribute注解的方法所在方法的controller内的所有方法在执行前都会被执行。因此对于一个controller映射多个URL的用法来说,要谨慎使用。
@ModelAttribute public void accessname(@cookievalue("Idea-216775ce") cookie cookie){ System.out.println(cookie.getName()); }
在该方法存在的Controller内的所有请求访问前都会输出
Idea-216775ce
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)