27.1. Spring Web MVC框架

Spring Web MVC框架(通常简称为”Spring MVC”)是一个富“模型,视图,控制器”web框架, 允许用户创建特定的@Controller@RestController beans来处理传入的HTTP请求,通过@RequestMapping注解可以将控制器中的方法映射到相应的HTTP请求。

示例:

  1. @RestController
  2. @RequestMapping(value="/users")
  3. public class MyRestController {
  4. @RequestMapping(value="/{user}", method=RequestMethod.GET)
  5. public User getUser(@PathVariable Long user) {
  6. // ...
  7. }
  8. @RequestMapping(value="/{user}/customers", method=RequestMethod.GET)
  9. List<Customer> getUserCustomers(@PathVariable Long user) {
  10. // ...
  11. }
  12. @RequestMapping(value="/{user}", method=RequestMethod.DELETE)
  13. public User deleteUser(@PathVariable Long user) {
  14. // ...
  15. }
  16. }

Spring MVC是Spring框架的核心部分,详细信息可以参考reference documentationspring.io/guides也有一些可用的指导覆盖Spring MVC。