Java 如何更正Spring MVC请求映射顺序?
我有一个运行以下RequestMapping的现有SpringMVC服务Java 如何更正Spring MVC请求映射顺序?,java,spring,spring-mvc,Java,Spring,Spring Mvc,我有一个运行以下RequestMapping的现有SpringMVC服务 @RequestMapping(value = "{user}/**", method = RequestMethod.GET) @RequestMapping(value = "api/{user}/**", method = RequestMethod.GET) 我现在需要使用以下RequestMapping向同一控制器添加一个新方法 @RequestMapping(value = "{user}/**", met
@RequestMapping(value = "{user}/**", method = RequestMethod.GET)
@RequestMapping(value = "api/{user}/**", method = RequestMethod.GET)
我现在需要使用以下RequestMapping向同一控制器添加一个新方法
@RequestMapping(value = "{user}/**", method = RequestMethod.GET)
@RequestMapping(value = "api/{user}/**", method = RequestMethod.GET)
当我针对新URL进行测试时,Spring总是选择第一个函数,以便
第二个功能无法访问
我是否可以让Spring使用我的新“api”方法?我能想到的唯一选择是在web.xml中创建一个新的Javaservlet,但我想做一些更简单的事情。我确实试过用
@Controller
和@Controller(“api”)
制作两个不同的控制器,但这并没有解决问题。假设用户
的参数是字符串
,Spring则假设您已被用户api
调用
有几种方法可以解决这个问题。第一个(也是最好的)是使您的请求映射更窄:完全指定映射,而不是依赖末尾的“**
”
第二种方法是禁止“api”用户在映射中使用。类似于以下的方法应该可以工作:
@RequestMapping(value = "{user:[^a][^p][^i]}/**", method = RequestMethod.GET)
更改方法定义的顺序有帮助吗?我不认为这有什么关系,但我只是尝试了一下,结果没有什么不同。我使用的控制器以“/”开始@RequestMapping值。因此,我希望看到@RequestMapping(value=“/api…@user636334),我对这个可能的解决方案的可能性感到兴奋,但它没有任何区别。使用什么处理程序的决定是在
org.springframework.util.AntPathMatcher.AntPatternComparator.compare(String,String)中编码的
我们需要**,因为路径部分的数量是可变的。我尝试了您建议的一种变体,效果很好。{user:^(?!api)。*}/**