Spring MVC缺少矩阵变量 [英] Spring MVC Missing matrix variable
问题描述
我正在尝试使用SpringMVC(从Spring boot 1.2.3RELEASE)向我的Rest Controller添加一个矩阵参数(或矩阵变量). 这是我的代码:
I'm trying to add a matrix parameter (or matrix variable) to my Rest Controller using SpringMVC (from Spring boot 1.2.3.RELEASE) Here is my code :
@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
Unfortunately, when I try to get : http://localhost:8080/subagents;agentName=hello
那是我收到的答案:
发生意外错误(类型=错误的请求,状态= 400).
There was an unexpected error (type=Bad Request, status=400).
缺少类型为String的方法参数的矩阵变量'agentName'
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 :-(
What did I do wrong ? According to http://docs.spring.io/spring-framework/docs/3.2.0.M2/reference/html/mvc.html that should work :-(
感谢您的回答!
推荐答案
作为链接到各州的文档,
As the documentation you linked to states,
请注意,要启用矩阵变量的使用,必须设置
RequestMappingHandlerMapping
的removeSemicolonContent
属性false
.默认情况下,它设置为true
(MVC除外) 命名空间和MVC Java配置都会自动启用 矩阵变量的使用.
Note that to enable the use of matrix variables, you must set the
removeSemicolonContent
property ofRequestMappingHandlerMapping
tofalse
. By default it is set totrue
with the exception of the MVC namespace and the MVC Java config both of which automatically enable the use of matrix variables.
如果要通过扩展WebMvcConfigurationSupport
配置应用程序,则覆盖requestMappingHandlerMapping
方法,该方法将准备RequestMappingHandlerMapping
并设置其适当的属性.
If you're configuring your application by extending WebMvcConfigurationSupport
, then override the requestMappingHandlerMapping
method which prepares the RequestMappingHandlerMapping
and set its appropriate property.
@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
final RequestMappingHandlerMapping requestMappingHandlerMapping = super.requestMappingHandlerMapping();
requestMappingHandlerMapping.setRemoveSemicolonContent(false); // <<< this
return requestMappingHandlerMapping;
}
然后您将一切准备就绪.
You'll then be all set.
使用Spring Boot,我想您所需要的就是用上述方法声明一个@Bean
方法.返回RequestMappingHandlerMapping
实例.
With Spring Boot, I think all you need is to declare a @Bean
method with the above, ie. that returns a RequestMappingHandlerMapping
instance.
这篇关于Spring MVC缺少矩阵变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!