Spring MVC缺少矩阵变量

编程入门 行业动态 更新时间:2024-10-20 16:04:12
本文介绍了Spring MVC缺少矩阵变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用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缺少矩阵变量

本文发布于:2023-11-08 13:55:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569565.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   变量   Spring   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!