preface

RFC3986 defines that the Path of a URI can contain a name-value fragment, extending the situation where optional parameters can be set only through a Query String. Suppose we need to design an API for “searching part of the optional information of some employees in a department”, we use query string and path name-value respectively to design comparison, and see the specific effect:

  1. To query a string:/api/v1/users/optional-info? dept=321&name=joh*&fields=hometown,birthProblem: The dept and Name belong to the Users path, and the fields belong to the optional-info path, but now they’re all squeezed into the query string.
  2. Path name-value:/api/v1/users/depts=321; name=joh*/optional-fields/fields=hometown,birth

You can see that the path name-value approach is logically more logical.

@matrixvariable Annotation property description

Before we start, let’s memorize the attributes of annotations.

  1. valueAnd attributespathVarThe alias;
  2. pathVarSpecifies the name of the path segment where the name-value parameter resides
  3. nameSpecifies the name of the name-value parameter
  4. requiredMandatory. The default value is false
  5. defaultValueSetting defaults

What are pathVar and name? Please continue to see the following examples, you will understand in seconds!

Enable @ MatrixVariable

Although the @MatrixVariable feature has been supported since Spring 3.2, it is still disabled by default. We need to manually enable it to use.

@Configuration public class SpringBootConfig implements WebMvcConfigurer { @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); }}Copy the code

Gameplay with only one parameter value

Note: Multiple name-values are separated by semicolons (;), for example, name=joh*; Dept = 321.

/* 1. Obtain the parameter request URI of a single path fragment as/demo2/66. color=red; year=2020 */ @RequestMapping(path="/Demo1/{id}", method=RequestMethod.GET) public String test1(@PathVariable String id, @MatrixVariable String color, @MatrixVariable String year){} /* 2. /Demo2/color=red; year=2020 */ @RequestMapping(path="/Demo1/{id}", method=RequestMethod.GET) public String test2(@MatrixVariable String color, @MatrixVariable String year){} /* 3. The request URI for parameters in different path fragments is/demo2/66; color=red; year=2020/pets/77; color=blue; year=2019 */ @RequestMapping(path="/Demo2/{id1}/pets/{id2}", method=RequestMethod.GET) public String test3(@PathVariable String id1, @PathVariable String id2, @MatrixVariable(name="color", pathVar="id1") String color1, @MatrixVariable(name="year", pathVar="id1") String year1, @MatrixVariable(name="color", pathVar="id2") String color2, @MatrixVariable(name="year", pathVar="id2") String year2){} /* 4. /Demo2/color=red; year=2020/pets/77; color=blue; year=2019 */ @RequestMapping(path="/Demo2/{id1}/pets/{id2}", method=RequestMethod.GET) public String test4(@PathVariable String id2, @MatrixVariable(name="color", pathVar="id1") String color1, @MatrixVariable(name="year", pathVar="id1") String year1, @MatrixVariable(name="color", pathVar="id2") String color2, @MatrixVariable(name="year", pathVar="id2") String year2){} /* 5. @requestMapping (path="/Demo3/{id1}/pets/{id2}", method=RequestMethod.GET) public String test5(@MatrixVariable Map<String, Object> all, @MatrixVariable(pathVar="id1") Map<String, Object> mapId1) {}Copy the code

Gameplay where parameters have multiple values

If the parameter value is not single, it can be passed in two ways:

  1. Values are separated by commas, as inDept = 321123
  2. The same name-value pair, such asdept=321; dept=123
*/ @requestMapping (path="/Demo1/{id}", Method = requestmethod.get) public String test1(@matrixvariable Integer[] color){} /* Request = /Demo1/color; */ @requestMapping (path="/Demo1/{id}", method=RequestMethod.GET) public String test1(@MatrixVariable Integer[] color){}Copy the code

Those pits to watch out for

In the case of multiple parameter values, there are also the following 3 pits, please pay attention to:

  1. StringParameter types can accept all values passed by commas and by the same name name-value, while other types can only get the first value.
*/ @requestMapping (path="/Demo1/{id}", Method = requestmethod.get) public String test1(@matrixvariable String color){} /* request is /Demo1/color=123; */ @requestMapping (path="/Demo1/{id}", Method = requestmethod.get) public String test1(@matrixvariable String color){} /* request is /Demo1/color=123; */ @requestMapping (path="/Demo1/{id}", method=RequestMethod.GET) public String test1(@MatrixVariable Integer color){}Copy the code
  1. Map<String, Object[]>You can only get the first value of a parameter.
*/ @requestMapping (path="/Demo1/{id}", method=RequestMethod.GET) public String test1(@MatrixVariable Map<String, Integer[]> color){}Copy the code
  1. If parameters with the same name appear in different path segments, the path of all the same parameters must be identified by pathVar; otherwise, URI matching fails.
// The following handler identifies only the second parameter pathVar, but does not identify the first parameter, which will also fail to match. @RequestMapping(path="/Demo2/{id1}/pets/{id2}", method=RequestMethod.GET) public String test2(@MatrixVariable String color, @MatrixVariable(name="color", pathVar="id2") String color2){}Copy the code

conclusion

I will write here today, and there will be more Spring Boot sharing in the future. Please pay more attention to me! Reprint please indicate the from: www.cnblogs.com/fsjohnhuang… – fat, John