使用Spring Boot框架时,一般在Controller层处理收到的HTTP请求,创建一个Java类,加上@RestController注解。
1 请求类型
1.1 @RequestMapping
@RequestMapping是一个通用的注解,支持处理所有的HTTP请求,通过设置method属性指定接收HTTP请求的类型,包括RequestMethod.POST、RequestMethod.GET、RequestMethod.PUT和RequestMethod.DELETE等,示例如下
@RequestMapping(value = "/user/login", method = RequestMethod.POST)
public int login(@RequestBody User user)
1.2 组合注解
还可以直接使用组合注解(RequestMapping的变形)来指定请求类型。组合注解是方法级别的,只能用在方法上。4个组合注解如下:
@GetMapping = @RequestMapping(method = RequestMethod.GET)
@PostMapping = @RequestMapping(method = RequestMethod.