1.路径设置
@RequestMapping(value="地址",method="请求方式") -> 可以加在【类】和【方法】上
@GetMapping / @PostMapping / @PutMapping / @DeleteMapping -> 只能加在方法上
get @GetMapping == @RequestMapping(xxx, RequestMethod=GET)
post @PostMapping == @RequestMapping(xxx, RequestMethod=POST)
put @PutMapping == @RequestMapping(xxx, RequestMethod=PUT)
delete @DeleteMapping == @RequestMapping(xxx, RequestMethod=DELETE)
2.接收参数【重点】
2.1.param
直接接收 handler(类型 形参名) 形参名 = 请求参数名
注解指定 handler(@RequestParam(name="请求参数名", required=true, defaultValue="默认值"))
一名多值 handler(@RequestParam List key)
实体接收 handler(实体 对象) 对象的属性名 = 请求参数名
2.2.路径参数
设置动态路径和标识 /{key}/info/{key}
接收路径 handler(@PathVariable(动态路径key) 类型 形参名)
2.3.json
数据接收 handler(@RequestBody 实体类 对象)
准备工作:
1.导入jackson依赖
2.@EnableWebMvc [加入handlerMapping 加入handlerAdapter 给handlerAdapter配置json处理器]
3.cookie接收
handler(@CookieValue("cookie的名字"))
4.请求头接收
handler(@RequestHeader("cookie的名字"))
5.原生api获取
handler(HttpServletResponse response, HttpServletRequest request, HttpSession session)
ServletContext -> ioc -> 全局变量 @Autowired
6.共享域获取
原生api方式
SpringMVC接收参数的方式
最新推荐文章于 2024-10-13 23:41:00 发布
博客主要介绍了Spring中路径设置和接收参数的相关内容。路径设置方面,介绍了@RequestMapping及@GetMapping等注解的使用。接收参数部分是重点,涵盖了param、路径参数、json、cookie、请求头、原生api和共享域获取等多种方式,还提及了接收json数据的准备工作。

1361

被折叠的 条评论
为什么被折叠?



