Spring MVC缺少矩阵变量

我正在尝试使用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

这就是我收到的答案:

出现意外错误(type = Bad Request,status = 400)。

缺少类型为String的方法参数的矩阵变量’agentName’

我做错了什么 ? 根据应该工作的http://docs.spring.io/spring-framework/docs/3.2.0.M2/reference/html/mvc.html 🙁

谢谢你的回答!

作为您链接到州的文档,

请注意,要启用矩阵变量,必须将RequestMappingHandlerMappingremoveSemicolonContent属性设置为false 。 默认情况下,它设置为true ,但MVC命名空间和MVC Java配置除外,它们都自动启用矩阵变量。

如果您通过扩展WebMvcConfigurationSupport来配置应用程序,则覆盖requestMappingHandlerMapping方法,该方法准备RequestMappingHandlerMapping并设置其适当的属性。

 @Override public RequestMappingHandlerMapping requestMappingHandlerMapping() { final RequestMappingHandlerMapping requestMappingHandlerMapping = super.requestMappingHandlerMapping(); requestMappingHandlerMapping.setRemoveSemicolonContent(false); // <<< this return requestMappingHandlerMapping; } 

然后你就可以了。


使用Spring Boot,我认为您需要的是使用上面的方法声明一个@Bean方法,即。 返回RequestMappingHandlerMapping实例。

在SpringBoot应用程序中为了启用Matrix变量,您需要定义下面的覆盖代码

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

否则,它们默认是禁用的