我正在尝试使用 SpringMVC(来自 Spring boot 1.2.3.RELEASE)将矩阵参数(或矩阵变量)添加到我的 Rest Controller 这是我的代码:
@RestController
public class SubAgentsController {
@RequestMapping(value = "/{subagents}", method = RequestMethod.GET)
public SubAgent subAgents(@MatrixVariable(value="agentName", pathVar="subagents") String agentName) {
System.out.println(agentName);
}
}
不幸的是,当我试图获得: http://localhost:8080/subagents;agentName=hello
这是我收到的答案:
There was an unexpected error (type=Bad Request, status=400).
Missing matrix variable 'agentName' for method parameter of type String
我做错了什么?根据http://docs.spring.io/spring-framework/docs/3.2.0.M2/reference/html/mvc.html那应该有效:-(
感谢您的回答!
最佳答案
在 Spring Boot 应用程序中,为了启用 Matrix 变量,您需要定义以下覆盖代码
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
否则,默认情况下它们是禁用的
关于java - Spring MVC 缺少矩阵变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30539783/