HTTP方法之@RequestMapping与Spring组合注解比较 - Rahul


@RequestMapping 是在 Spring 2.5 中引入的:

@RequestMapping(value = "/student/{studentId}/marks/determine",
                    method = RequestMethod.GET,
                    produces = { MediaType.APPLICATION_JSON_VALUE })
The
"method" in the HTTP request maps to the below RequestMethod:
GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.

从Spring 4.3开始,spring 框架为我们提供了对应 HTTP 方法类型的新的快捷方式,称为组合注解:
@GetMapping是一个组合注解,作为@RequestMapping(method = RequestMethod.GET)的快捷方式。

现在,去掉@RequestMapping并显式声明HTTP方法类型:

@GetMapping(value = "/student/{studentId}/marks/determine",produces = { MediaType.APPLICATION_JSON_VALUE })

其他常用的组合注解是:

  • @PostMapping是一个组合注解,作为@RequestMapping(method = RequestMethod.POST) 的快捷方式
  • @PutMapping是一个组合注解,作为@RequestMapping(method = RequestMethod.PUT) 的快捷方式
  • @DeleteMapping是一个组合注解,作为@RequestMapping(method = RequestMethod.DELETE) 的快捷方式