Spring MVC提供了许多注解来简化和规范Web应用程序的开发。以下是一些常用的Spring MVC注解及其用途:
控制器相关注解
-
@Controller
- 标识一个类为Spring MVC控制器,用于处理HTTP请求。
import org.springframework.stereotype.Controller; @Controller public class MyController { // ... }
-
@RestController
- 标识一个类为RESTful Web服务的控制器,相当于
@Controller
和@ResponseBody
的结合。
import org.springframework.web.bind.annotation.RestController; @RestController public class MyRestController { // ... }
- 标识一个类为RESTful Web服务的控制器,相当于
请求映射相关注解
-
@RequestMapping
- 用于映射请求URL到处理方法或类,可以用于类级别和方法级别。
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class ApiController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public String sayHello() { return "Hello, World!"; } }
-
@GetMapping
- 专用于处理HTTP GET请求,是
@RequestMapping(method = RequestMethod.GET)
的快捷方式。
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @GetMapping("/hello") public String sayHello() { return "Hello, World!"; } }
- 专用于处理HTTP GET请求,是
-
@PostMapping
- 专用于处理HTTP POST请求,是
@RequestMapping(method = RequestMethod.POST)
的快捷方式。
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @PostMapping("/submit") public String handleSubmit() { return "Form Submitted!"; } }
- 专用于处理HTTP POST请求,是
-
@PutMapping
- 专用于处理HTTP PUT请求,是
@RequestMapping(method = RequestMethod.PUT)
的快捷方式。
import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @PutMapping("/update") public String handleUpdate() { return "Updated!"; } }
- 专用于处理HTTP PUT请求,是
-
@DeleteMapping
- 专用于处理HTTP DELETE请求,是
@RequestMapping(method = RequestMethod.DELETE)
的快捷方式。
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @DeleteMapping("/delete") public String handleDelete() { return "Deleted!"; } }
- 专用于处理HTTP DELETE请求,是
请求参数和响应处理相关注解
-
@RequestParam
- 用于绑定请求参数到方法参数。
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @GetMapping("/greet") public String greet(@RequestParam(name = "name", required = false, defaultValue = "World") String name) { return "Hello, " + name + "!"; } }
-
@PathVariable
- 用于将URL中的路径变量绑定到方法参数。
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @GetMapping("/user/{id}") public String getUserById(@PathVariable("id") String id) { return "User ID: " + id; } }
-
@RequestBody
- 用于将请求体绑定到方法参数,通常用于处理JSON或XML请求体。
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @PostMapping("/json") public String handleJson(@RequestBody String json) { return "Received JSON: " + json; } }
-
@ResponseBody
- 用于将方法返回值直接作为HTTP响应体返回,通常用于返回JSON或XML数据。
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @GetMapping("/message") @ResponseBody public String getMessage() { return "Hello, World!"; } }
其他常用注解
-
@ModelAttribute
- 用于绑定请求参数到模型对象,并在视图中使用该对象。
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MyController { @RequestMapping("/form") public String showForm(@ModelAttribute("user") User user) { return "userForm"; } }
-
@SessionAttributes
- 用于将模型中的属性暂存到HTTP会话中。
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.SessionAttributes; @Controller @SessionAttributes("user") public class MyController { @RequestMapping("/login") public String login(@ModelAttribute("user") User user) { return "loginSuccess"; } }
这些注解极大地简化了Spring MVC应用程序的开发,使代码更简洁、易读,并提高了开发效率。