“This is the 20th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
1. @ RequestMapping
The main purpose of the @RequestMapping annotation is to map Web requests to methods in the request processing class. Spring MVC and Spring WebFlux through RquestMappingHandlerMapping and RequestMappingHndlerAdapter two classes to provide support for @ RequestMapping annotations.
The @requestMapping annotation annotates the request handling methods in the request handling class. The @requestMapping annotation has the following six configuration attributes:
• value: request URL for the mapping or its alias
• Method: indicates the HTTP compatible method name
• Params: Filters requests based on the presence, default, or value of HTTP parameters
• Header: Filters requests based on the existence, default, or value of HTTP headers
• Consume: Sets the type of media allowed in the body of an HTTP request
• Product: media type allowed in the HTTP response body
Tip: Before using @requestMapping, the request processing class also needs to be marked with @Controller or @RestController
Here are two examples of using @requestMapping:
@RequestMapping can also mark a class so that when a method in the class maps the request path, it will automatically concatenate the value set by @RequestMapping in the class before the mapping path in the method.
2, @ RequestBody
The @requestbody is used in the parameter list of the processing request method to bind the parameters in the RequestBody to an object. The RequestBody parameters are passed through HttpMessageConverter, matching and binding values based on the parameter names in the RequestBody to the attribute names of the object. In addition, parameters in the request body can be validated using the @valid annotation.
3, @ GetMapping
The @getMapping annotation is used to process HTTP GET requests and map requests to specific processing methods. Specifically, @getMapping is a composite annotation that is equivalent to a shortcut to @requestMapping (method= requestMethod.get).
4, @ PostMapping
The @postMapping annotation is used to process HTTP POST requests and map requests to specific processing methods. @postMapping, like @getMapping, is a composite annotation that works as a shortcut to @requestMapping (method= httpmethod.post).
5, @ PutMapping
The @putMapping annotation is used to process HTTP PUT requests and map the requests to specific processing methods. The @putMapping annotation is a composite annotation that is equivalent to a shortcut to @RequestMapping(Method = httpMethod.put).
6, @ DeleteMapping
The @deletemapping annotation is used to process HTTP DELETE requests and map them to DELETE methods. @deletemapping is a composite annotation that is equivalent to a shortcut to @requestMapping (method= httpmethod.delete).
7, @ PatchMapping
The @PatchMapping annotation is used to process HTTP PATCH requests and map the requests to corresponding processing methods. PatchMapping is a shortcut to @requestMapping (method= httpmethod.patch).
8, @ ControllerAdvice
@ControllerAdvice is an extension of the @Component annotation, and Spring automatically scans and detects classes flagged by @ControllerAdvice. @ControllerAdvice is used with @ExceptionHandler, @initBinder, and @ModelAttribute annotations to handle exceptions thrown by controllers. First, we need to define a class annotated by @ControllerAdvice, in which we define a method to handle the specific exception and mark it with the @ExceptionHandler annotation. In addition, @initBinder can be used to configure classes globally when necessary, and @ModelAttribute can be used to configure view-specific parameters. Using the @controllerAdvice annotation, you can quickly create unified, custom exception handling classes.
9, @ ResponseBody
@responseBody automatically writes the return value of the method in the controller to the HTTP response. Specifically, the @responseBody annotation can only be used in a class marked by the @Controller annotation. If you are in a class marked by @RestController, the method does not need to be annotated with the @responseBody annotation. @restController is kind of a combination annotation of @Controller and @responseBody.
10, @ ExceptionHandler
The @ExceptionHander annotation is used to annotate methods that handle exceptions thrown by a particular type of exception class. When a method in a controller throws an exception, Spring automatically catches the exception and passes the caught exception information to the method annotated by @ExceptionHandler.
11, @ ResponseStatus
The @responseStatus annotation annotates the request handling method. Using this annotation, you can specify the HTTP STATUS required for the response. In particular, we can assign the value attribute of the annotation using the HttpStauts class.
12, @ PathVariable
The @pathVariable annotation binds parameters in a method to template variables in the request URI. You can specify the template variables of the URI using the @requestMapping annotation, and then bind the parameters in the method to the template variables using the @pathVariable annotation. In particular, the @pathVariable annotation allows us to alias a parameter using the value or name attribute.
Template variable names need to be wrapped with “{}”. If the method parameter name is the same as the URI template variable name, the alias definition can be omitted in @pathVariable.
Tip: If the argument is an optional, optional option, set require = false in @pathVariable
13, @ RequestParam
The @requestParam annotation is used to bind the parameters of a method to the parameters passed in the Web request. You can easily access the values of HTTP request parameters using @requestParam.
The other attributes of this annotation are configured the same as @pathvariable, in particular, a defaultValue can be set using defaultValue if the parameter passed is empty.
14, @ Controller
The @Controller is an extension of the @Component annotation, and Spring automatically scans and configures the classes annotated by the annotation. This annotation is used to annotate the Spring MVC controller.
15, @ RestController
The @restController annotation, introduced in Spring 4.0, is a controller specific annotation. This annotation acts as a shortcut to @Controller and @responseBody. When using this annotation, you no longer need to use the @responseBody annotation on the method.
16, @ ModelAttribute
With this annotation, you can access the model that already exists in the controller by the model index name.
As with the @PathVariable and @RequestParam annotations, you do not have to specify an index name if the parameter name has the same name as the model.
In particular, if you annotate a method with @ModelAttribute, Spring binds the return value of the method to a specific Model.
All methods annotated by the @ModelAttribute annotation will be executed before Spring invokes the specific processing method.
17, @ CrossOrigin
The @Crossorigin annotation provides cross-domain invocation support for request handling classes or request handling methods. If we annotate the class with this annotation, all methods in the class gain cross-domain support. The advantage of using this annotation is that you can fine-tune cross-domain behavior.
18, @ InitBinder
The @initBinder annotation is used to annotate methods that initialize WebDataBinider for processing form data passed through Http requests, such as time formatting, string manipulation, and so on.