SpringBoot常用的请求映射方式注解
我们在处理web端应用的请求时,通常会使用如下几种方式进行请求映射,我们可以通过查看源码看到它们的真实面目。
- @RequestMapping
源码对注解的注释:Annotation for mapping web requests onto methods in request-handling classes with flexible method signatures.
翻译:用于将web请求映射到具有灵活方法签名的请求处理类中的方法的注释。
- @GetMapping
源码对注解的注释:Annotation for mapping HTTP GET requests onto specific handler methods.
Specifically, @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).
翻译:用于将HTTP GET请求映射到特定的请求处理方法的注释。具体来说,@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。
- @PostMapping
源码对注解的注释:Annotation for mapping HTTP POST requests onto specific handler methods.
Specifically, @PostMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.POST).
翻译:用于将HTTP POST请求映射到特定的请求处理方法的注释。具体来说,@PostMapping是一个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写。
- @PutMapping
源码对注解的注释:Annotation for mapping HTTP PUT requests onto specific handler methods.
Specifically, @PutMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.PUT).
翻译:用于将HTTP PUT请求映射到特定的请求处理方法的注释。具体来说,@PutMapping是一个组合注解,是@RequestMapping(method = RequestMethod.PUT)的缩写。
- @DeleteMapping
源码对注解的注释:Annotation for mapping HTTP DELETE requests onto specific handler methods.
Specifically, @DeleteMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.DELETE).
翻译:用于将HTTP DELETE请求映射到特定的请求处理方法的注释。具体来说,@DeleteMapping是一个组合注解,是@RequestMapping(method = RequestMethod.DELETE)的缩写。
- @PatchMapping
源码对注解的注释:Annotation for mapping HTTP PATCH requests onto specific handler methods.
Specifically, @PatchMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.PATCH).
翻译:用于将HTTP PATCH请求映射到特定的请求处理方法的注释。具体来说,@PatchMapping是一个组合注解,是@RequestMapping(method = RequestMethod.PATCH)的缩写。
从源码注释和注解接口实现源代码可以得出以下结论:
- 可以使用@RequestMapping注解并指定method属性对所有HTTP进行映射。
- @XxxMapping注解是@RequestMapping(method=”XXX”)的缩写
- @GetMapping是@RequestMapping(method = RequestMethod.GET)的缩写
- @PostMapping是@RequestMapping(method = RequestMethod.POST)的缩写
- @PutMapping是@RequestMapping(method = RequestMethod.PUT)的缩写
- @DeleteMapping是@RequestMapping(method = RequestMethod.DELETE)的缩写
- @PatchMapping是@RequestMapping(method = RequestMethod.PATCH)的缩写
- 推荐用法:在对特定的请求进行映射时,采用对应的注解。