我正在尝试使用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); } }不幸的是,当我尝试获得: localhost:8080/subagents; agentName = hello
Unfortunately, when I try to get : 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
我做错了什么?根据 docs.spring可以正常工作的.io/spring-framework/docs/3.2.0.M2/reference/html/mvc.html :-(
What did I do wrong ? According to 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 of RequestMappingHandlerMapping to false. By default it is set to true 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缺少矩阵变量
发布评论