Spring Rest路径与@PathVariable和@MatrixVariable冲突

时间:2017-09-13 06:58:02

标签: java spring rest

我正在使用Spring 4.1.9.RELEASE

配置下一个Rest Controller。

并创建了/ get / {id}以某种方式隐藏/ get / matrix / {vars}的映射

当我尝试通过下一个网址http://localhost:8080/testMatrixVariables/get/matrix/;v1=1;v2=2;v3=3拨打电话时 我从调试日志

获得

org.springframework.beans.TypeMismatchException:无法将[java.lang.String]类型的值转换为必需的类型[long];嵌套异常是java.lang.NumberFormatException:对于输入字符串:“matrix”

似乎从我的网址尝试解析{id}但不是/ get / matrix / {vars}

我同时下次通话工作完美 http://localhost:8080/testMatrixVariables/getMatrix/;v1=1;v2=2;v3=3http://localhost:8080/testMatrixVariables/get/1 工作得很好 和 http://localhost:8080/testMatrixVariables/get/anotherPathVariable/2 也有效

只有当我们在@PathVariable和@MatrixVariable

之间发生冲突时才会出现问题

http://localhost:8080/testMatrixVariables/get/matrix/;v1=1;v2=2;v3=3

@RestController
@RequestMapping(value = "/testMatrixVariables")
public class TestController {

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public
    @ResponseBody
    ResponseEntity<String> getByPathVariable(@PathVariable("id") long v1) {
        return ResponseEntity.ok("ok" + v1);
    }

    @RequestMapping(value = "/get/anotherPathVariable/{id}", method = RequestMethod.GET)
    public
    @ResponseBody
    ResponseEntity<String> getAnotherPathVariableByPathVariable(@PathVariable("id") long v1) {
        return ResponseEntity.ok("ok" + v1);
    }


    @RequestMapping(value = "/getMatrix/{vars}", method = RequestMethod.GET)
    public @ResponseBody ResponseEntity<String> getMatrix(
            @MatrixVariable(pathVar = "vars", required = true) String v1,
            @MatrixVariable(pathVar = "vars", required = true) String v2,
            @MatrixVariable(pathVar = "vars", required = true) String v3) {
        return ResponseEntity.ok("ok" + v1 + v2 + v3);
    }

    @RequestMapping(value = "/get/matrix/{vars}", method = RequestMethod.GET)
    public @ResponseBody ResponseEntity<String> getGetMatrix(
            @MatrixVariable(pathVar = "vars", required = true) String v1,
            @MatrixVariable(pathVar = "vars", required = true) String v2,
            @MatrixVariable(pathVar = "vars", required = true) String v3) {
        return ResponseEntity.ok("ok" + v1 + v2 + v3);
    }

}

3 个答案:

答案 0 :(得分:1)

这不起作用,因为默认情况下禁用Matrix变量。 它可以通过两种方式启用:

  1. 将RequestMappingHandlerMapping的removeSemicolonContent属性设置为false。默认情况下,它设置为true。 例如:handler.setRemoveSemicolonContent(false);

  2. 或者,如果您使用的是xml配置,则可以将其作为

  3. 完成
    
    
    
    <mvc:annotation-driven enable-matrix-variables="true"/> 
    

答案 1 :(得分:0)

尝试放

@RequestMapping(value = "/get/matrix/{vars}", method = RequestMethod.GET)
public @ResponseBody ResponseEntity<String> getGetMatrix(
        @MatrixVariable(pathVar = "vars", required = true) String v1,
        @MatrixVariable(pathVar = "vars", required = true) String v2,
        @MatrixVariable(pathVar = "vars", required = true) String v3) {
    return ResponseEntity.ok("ok" + v1 + v2 + v3);
}

以上

@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
public @ResponseBody ResponseEntity<String> getByPathVariable(@PathVariable("id") long v1) {
    return ResponseEntity.ok("ok" + v1);
}

应该首先以更窄的映射排序,然后是更广泛的映射。

答案 2 :(得分:0)

默认情况下,Spring会在分号后删除URI内容

根据documentation

  

请注意,要启用矩阵变量,必须将RequestMappingHandlerMapping的removeSemicolonContent属性设置为false。默认情况下,它设置为true。

这个对我有用(用你的例子):

@Configuration
public class WebConfig extends DelegatingWebMvcConfiguration {

    @Override
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        final RequestMappingHandlerMapping handlerMapping = super.requestMappingHandlerMapping();
        handlerMapping.setRemoveSemicolonContent(false);
        return handlerMapping;    
    }
}