gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:30:46 24 4
gpt4 key购买 nike

我正在使用 Spring 4.1.9.RELEASE

配置了下一个休息 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: For input string: "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);
}

}

最佳答案

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

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

  2. 或者,如果您使用 xml 配置,则可以这样做


 
<mvc:annotation-driven enable-matrix-variables="true"/> 

关于java - Spring Rest路径与@PathVariable和@MatrixVariable冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46190846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com