Request processing -SpringBoot common parameter annotations used

Comments:

  • @pathvariable PathVariable
  • @requestheader gets the RequestHeader
  • @requestParam retrieves request parametersurl? a=1&b=2)
  • @cookievalue gets the Cookie value
  • @requestAttribute Obtains the request domain attribute
  • @requestbody fetch RequestBody [POST]
  • @matrixvariable Matrix variables
  • @ModelAttribute

1. @pathVariable annotations

This annotation is mainly used for rest style collocation. Request parameters and values are no longer given in the form of K: v in the request path. I’m just going to give you a value. If the method argument is a Map

will contain all the variables and values in the path.
,>

Access: The browser input path variables, the following is a rest style get request, directly in the address bar request is a GET request

Get the request parameters on all access paths: localhost:8080/ car/{id}/owner/{username}? age=19&inters=

2. @requestheader annotations

This annotation is used to retrieve the data in the request header. The client can retrieve some of the parameters in the header after the request. Support for traditional SpringMVC as well as WebFlux responsiveness. If the method argument is a Map

will contain all the request headers and values
,>

@GetMapping("user/RequestHeader")
public Map<String,Object> getUser3(@RequestHeader("Accept") String Accept,
                                   @RequestHeader Map<String,String> header){
    Map<String,Object> map=new HashMap<>();
    map.put("Accept",Accept);
    map.put("header",header);
    return map;
}
Copy the code

Testing:

3. @requestParam annotations

It is used to get the request parameter name, set whether the parameter is optional, and the default value.

Address: blog.csdn.net/weixin_4380…

4. @cookievalue annotated

It is used to obtain Cookie values

5. @requestAttribute annotations

If the page cannot be directly redirected (for example, the SUCCESS page in web-INF), you can use the forwarding method. When forwarding, the request parameters can be carried in the request, and the forward will carry the parameters of the previous request (a complete request includes forwarding).

Since it is the same request, it is also possible to get the native HttpServletRequest directly, and then take parameters and properties from it as well.

The following is an idea of forwarding

The results of

6. @requestBody annotation

Mainly get form or Ajax submitted content, the form submitted parameters and values to get all out. The ability to get the RequestBody (so the request must be a post request –@PostMapping) is typically used with the @requestbody annotation to map parameters to the pojo class, but ensure that the parameter names passed in are the same

@matrixVariable vs. UrlPathHelper

7.1 Basic Introduction

These are the three most common types of requests; Matrix variable requests are a new style of request. Strictly speaking, matrix variable requests require rest style but are different from REST.

Interviewer: Cookies are disabled during page development. How to use (find) the contents of session?

  • Normal use of cookies: session.set(a,b)→ jsessionID → Cookie → Carry this value every time a request is sent
  • Each user has a different sessionID, we call it jsessionID, jsessionID will be stored in cookies, we users will carry cookies when sending requests. Therefore, if cookies are disabled, objects stored inside cannot be retrieved.
  • Resolve the case where cookies are disabled: assume the access path/ABC. We can carry it in terms of matrix variablesjsessionidValue:/abc; jsessionid=xxx. The above process is the process of URL rewriting, which is equivalent to passing the cookie value in the form of matrix variables.

7.2 MatrixVariable annotations

This annotation is an annotation that modifies parameters and can be loaded by the JVM at runtime.

  • The value and name attributes are the same attributes used to bind the parameters to the request.
  • Pathvar is an important attribute. When the same parameter appears in a multilevel path, you can use pathvar to specify which level of path to bind, and then obtain the parameters at that level.
  • The required attribute mainly modifies whether the current matrix variable is required; True means necessary, false means not necessary.
  • DefaultValue attribute when the required attribute value is false, no value given for this parameter in the matrix variable of the submitted request can be replaced by the defaultValue.

7.3. Details of use

Because SpringBoot does not support matrix variables by default, it turns matrix variables off. Therefore, we need to make manual configuration changes to SpringBoot auto-assembled Bean objects at the time of use.

7.3.1 WebMvcAutoConfiguration automatic assembly

  • The WebMvcAutoConfiguration auto-assembly class is automatically assembled when SpringBoot is started;
  • The inner class WebMvcAutoConfigurationAdapter adapter implements all the methods in the WebMvcConfigurer interface.
  • All methods in the WebMvcConfigurer interface are implemented by default and are new to JDK8.

7.3.2, UrlPathHelper class

What really makes it impossible to use matrix variables is that removeSemicolonContent in the UrlPathHelper class defaults to true, which removes semicolons.

  • Since MatrixVariable variables are separated by semicolons, the underlying default is removed, which means that they do not take effect.
  • Therefore, manual startup principle: For path processing, SpringBoot uses UrlPathHelper for parsing, and its property removeSemicolonContent is set to false to enable it to support matrix variables.

7.3.3 Manually configure matrix variables

Methods: In order to enable the use of matrix variables, the WebMvcConfigurer interface must be implemented. The manual implementation of the configurePathMatch method is complete. All methods in the WebMvcConfigurer interface are default methods of default, so you can override them individually using adapter mode. So you can manually configure a Bean object in the IOC container in the configuration class that just overrides the implementation of this method.

Rewrite the following

7.4. Test use

First, support for matrix variables must be manually configured, and second, matrix variables are bound to REST path variables

7.4.1 Test 1

Both paths are handled using the same Controller, and path takes/to the first one; The path between.

7.4.2 Test 2

Age in Gage binding {gege}, age in Dage binding {didi}; This will tell you exactly which age to get.

7.4.3 Test 3

8. Supplement: SpringBoot realizes several common ways of front and back end data interaction, JSON data interaction and Controller receiving parameters

From: blog.csdn.net/qq_20957669…

Most Internet projects today are developed in a separate way, with the front end responsible for page presentation and data acquisition, and the back end responsible for business logic processing and interface encapsulation. When interacting with the front-end, json data is often used to interact with the front-end, so if you want to retrieve the JSON data from the front-end, you need to use the @requestBody annotation. The @RequestBody annotation reads the content of the HTTP request (string) and converts it into JSON, XML, and other data through the HttpMessageConverter interface provided by SpringMVC and binds it to the parameters of the Controller method.

When the submission mode is POST,

  • JQuery Ajax uploads JSON objects with Application/X-www-form-urlencoded, and the backend uses @requestParam or Servlet to get parameters.

  • JQuery Ajax uploads json strings as Application /json, and the back end uses @rquestBody to get parameters.

Get several common annotations for parameters

  • @pathVariable: Normally we use URI template mapping, that is, url/{param}, which is normally used with GET, DELETE, PUT methods, we can GET the parameters after the URL.

  • RequestParam @requestParam: Normally we use this annotation to retrieve multiple parameters, just write the parameter name in (), commonly used in PUT and POST.

  • @requestBody: This annotation is the same as @RequestParam. We use this annotation to convert all the parameters and pull them out one by one in the code section. This is the most common annotation I use to get parameters

  • @requestheader to get the values in the header, @cookievalue to get the Cookie values, and so on. I’m just going to show you some of the more common methods.