参考链接:
springboot各种注解.
@requestmapping参数produces,consumes.
一、@GetMapping是什么?
Spring4.3中引进了{@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@PatchMapping},来帮助简化常用的HTTP方法的映射,并更好地表达被注解方法的语义。
从命名约定我们可以看到每个注释都是为了处理各自的传入请求方法类型,即 @GetMapping 用于处理请求方法的 GET 类型, @ PostMapping 用于处理请求方法的 POST 类型等。
二、@GetMapping VS @RequestMapping
(1)@GetMapping 是在 @RequestMapping 基础上的封装
查看 @GetMapping 的源码可以发现,是在 @RequestMapping 的基础上进行了封装。
@Target({ java.lang.annotation.ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = { RequestMethod.GET })
public @interface GetMapping {
// abstract codes
...
}
其他几个注释也是一样的方式封装的。
(2)@GetMapping不支持consumes属性。
英文文档说法:@GetMapping does not support the consumes attribute of @RequestMapping.
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
consumes例子:
@Controller
@RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json")
public void addPet(@RequestBody Pet pet, Model model) {
// implementation omitted
...
}