Skip to content
Advertisement

How does pathVar Attribute of @MatrixVariable annotation works in Spring?

I was reading regarding the @Matrixvariable annotation in Spring doc Spring Doc

I have Understood this simple syntax // GET /pets/42;q=11;r=22

@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET)
public void findPet(@PathVariable String petId, @MatrixVariable int q) {

  // petId == 42
  // q == 11

}

but having problem in understanding the below snippet

// GET /owners/42;q=11;r=12/pets/21;q=22;s=23

@RequestMapping(value = "/owners/{ownerId}/pets/{petId}", method = RequestMethod.GET)
  public void findPet(
        @MatrixVariable Map<String, String> matrixVars,
        @MatrixVariable(pathVar="petId"") Map<String, String> petMatrixVars) {

    // matrixVars: ["q" : [11,22], "r" : 12, "s" : 23]
    // petMatrixVars: ["q" : 11, "s" : 23]

  }

What is this syntax @MatrixVariable(pathVar=”petId””) I haven’t understood the pathVar attribute of Matrixvariable annotation?

This line is ok for me // matrixVars: ["q" : [11,22], "r" : 12, "s" : 23] that this variable added with all the matrix variables. but how does petMatrixVars added with these Specific values mean

//petMatrixVars: ["q" : 11, "s" : 23]  ? why not  //petMatrixVars: ["q" : 22, "s" : 23]  ?

Thanks in Advance for your time spent on this thread!!

Advertisement

Answer

This is called Partial Binding it is used to get all variables from that segment in that path or if you want to get each variable from that path segment docs, and output is wrong in this documentation here

In your example you will get all variables that are in path after petId {21}

// GET /owners/42;q=11;r=12/pets/21;q=22;s=23
 @MatrixVariable(pathVar="petId") Map<String, String> petMatrixVars)

If you want to get only q after petId segment then

@MatrixVariable(value ="q",pathVar="petId") int q

Here is the example with output, for @MatrixVariable we need to enable them first

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

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

Controller with @requestmapping method

@RequestMapping(value = "/owners/{ownerId}/pets/{petId}", method = RequestMethod.GET)
    public void findPet(
        @MatrixVariable Map<String, String> matrixVars,
        @MatrixVariable(pathVar="petId") Map<String, String> petMatrixVars) {
        System.out.println(matrixVars);
        System.out.println(petMatrixVars);
    }
}

Request: http://localhost:8080/sample/owners/42;q=11;r=12/pets/21;q=22;s=23

Output:

{q=11, r=12, s=23}
{q=22, s=23}

And if i change @MatrixVariable Map<String, List<String>> matrixVars, the output is

{q=[11, 22], r=[12], s=[23]}
{q=22, s=23}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement