Spring 4.3 在 HTTP 请求映射方面做了一次重要的语义化改进,将原本通用的 @RequestMapping 按 HTTP 方法进行了细分。这样做的好处是代码意图一目了然,不再需要反复声明 method 属性。
主要新增了以下 5 个组合注解:
@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)
实际开发中,直接使用这些注解能显著减少样板代码。下面是一个简单的 Controller 示例:
@RestController
public class UserController {
@GetMapping("/users")
public List<User> getUsers() {
// 处理查询逻辑
return userService.findAll();
}
@PostMapping("/users")
public User createUser(@RequestBody User user) {
// 处理创建逻辑
return userService.save(user);
}
}
这种写法不仅可读性更强,也符合 RESTful 规范的习惯。如果你还在使用 Spring 4.3 及以上版本,建议优先采用这些专用注解。

