In Spring Boot, the Controller layer is a component used to process HTTP requests. The following are commonly used annotations in the Controller layer:
1、@RestController
Identify a class as a controller and make it support a RESTful API. It is a combined annotation of @Controller and @ResponseBody.
@Controller injects the currently modified class into the SpringBoot IOC container, so that the class is instantiated during the process of running from the project where the class is located.
@ResponseBody Its function is short and short, it refers to the data returned by all API interfaces in this class, regardless of whether your corresponding method returns a Map or other Object, it will be returned to the client in the form of a Json string
@RestController
public class UserController {
// Controller methods
}
2、@RequestMapping
Map HTTP requests to handler method or controller class level. Can be used in class-level annotations to define basic URL paths, and further paths can be added in method-level annotations.
@RestController
@RequestMapping("/users")
public class UserController {
// Methods with specific request mappings
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
// Method implementation
}
@PostMapping
public User createUser(@RequestBody User user) {
// Method implementation
}
}
3、@GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @PatchMapping
Map HTTP GET, POST, PUT, DELETE and PATCH requests to processing methods respectively.
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
// Method implementation
}
@PostMapping
public User createUser(@RequestBody User user) {
// Method implementation
}
@PutMapping("/{id}")
public User updateUser(@PathVariable Long id, @RequestBody User user) {
// Method implementation
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable Long id) {
// Method implementation
}
@PatchMapping("/{id}")
public User partialUpdateUser(@PathVariable Long id, @RequestBody UserPartialUpdateRequest request) {
// Method implementation
}
}
4、@PathVariable
Used to bind the placeholder parameters in the URL path to the parameters of the processing method
@GetMapping("/users/{id}")
public User getUserById(@PathVariable Long id) {
// Method implementation
}
5、@RequestParam
Used to bind request parameters to the parameters of the processing method. You can specify the name of the parameter, whether it is required, and a default value.
@GetMapping("/users")
public List<User> getUsersByRole(@RequestParam("role") String role) {
// Method implementation
}
6、@RequestBody
It is used to bind the data in the request body to the parameters of the processing method, usually used to process the JSON data of the POST request.
@PostMapping("/users")
public User createUser(@RequestBody User user) {
// Method implementation
}
7、@RequestHeader
Used to bind the information in the request header to the parameters of the processing method.
@GetMapping("/users")
public List<User> getUsersByLocale(@RequestHeader("Accept-Language") String locale) {
// Method implementation
}
8、@ResponseBody
Return the method's return value directly as the content of the HTTP response instead of parsing it into a view.
@GetMapping("/users/{id}")
@ResponseBody
public User getUserById(@PathVariable Long id) {
// Method implementation
}
9、@ResponseStatus
Set the HTTP status code of the response.
@DeleteMapping("/users/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteUser(@PathVariable Long id) {
// Method implementation
}
10、@ModelAttribute
Used to bind request parameters to a model object and make it accessible in the view.
@GetMapping("/users/{id}")
public String getUserDetails(@PathVariable Long id, @ModelAttribute("message") String message) {
// Method implementation
}
11、@Valid
It is used to verify the binding request parameters, and perform data verification in combination with the JSR-303 Bean Validation specification.
@PostMapping("/users")
public ResponseEntity<?> createUser(@Valid @RequestBody User user, BindingResult result) {
if (result.hasErrors()) {
// Handle validation errors
}
// Method implementation
}