【问题标题】:How does pathVar Attribute of @MatrixVariable annotation works in Spring?@MatrixVariable 注解的 pathVar 属性在 Spring 中是如何工作的?
【发布时间】:2018-09-15 04:03:29
【问题描述】:

我正在阅读 Spring doc Spring Doc 中的 @Matrixvariable 注释

我已经理解了这个简单的语法 // 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

}

但在理解下面的 sn-p 时遇到问题

// 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]

  }

这个语法是什么@MatrixVariable(pathVar="petId"") 没看懂Matrixvariable注解的pathVar属性?

这条线对我来说没问题// matrixVars: ["q" : [11,22], "r" : 12, "s" : 23] 这个变量添加了所有矩阵变量。 但是添加这些特定值的petMatrixVars是什么意思

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

在此先感谢您在此线程上花费的时间!!

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    这称为Partial Binding,它用于从该路径中的该段获取所有变量,或者如果您想从该路径段获取每个变量docs,并且此文档中的输出错误here

    在您的示例中,您将获得 petId {21}

    之后路径中的所有变量
    // GET /owners/42;q=11;r=12/pets/21;q=22;s=23
     @MatrixVariable(pathVar="petId") Map<String, String> petMatrixVars)
    

    如果您只想在petId 段之后获得q,那么

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

    这是带有输出的示例,对于@MatrixVariable,我们需要先启用它们

    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);
        }
    }
    

    带有@requestmapping 方法的控制器

    @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);
        }
    }
    

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

    输出:

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

    如果我改变@MatrixVariable Map&lt;String, List&lt;String&gt;&gt; matrixVars, 输出是

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

    【讨论】:

    • 感谢您的第一时间回复。 “在您的示例中,您将获得 petId {21} 之后路径中的所有变量”,那么在这种情况下,petMatrixVars: ["q" : 22, "s" : 23] 是正确的,而不是 // petMatrixVars: ["q" : 11, "s" : 23] 根据你的说法?
    • 我检查并确认文档的值是错误的,输出值有错误,它的工作就像我说的@JAVA
    猜你喜欢
    • 1970-01-01
    • 2011-04-01
    • 2015-09-29
    • 2012-12-01
    • 2017-06-27
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    相关资源
    最近更新 更多