如何更正Spring MVC RequestMapping顺序? [英] How to correct Spring MVC RequestMapping order?

查看:268
本文介绍了如何更正Spring MVC RequestMapping顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行以下RequestMapping的现有Spring MVC服务.

I have an existing Spring MVC service running with the following RequestMapping.

@RequestMapping(value = "{user}/**", method = RequestMethod.GET)

我现在需要使用以下RequestMapping将新方法添加到同一控制器.

I now need to add a new method to the same controller with the following RequestMapping.

@RequestMapping(value = "api/{user}/**", method = RequestMethod.GET)

当我针对新的URL进行测试时,Spring总是选择第一个函数,因此 第二个功能无法访问.

When I test against the new URL, Spring is always choosing the first function so that this second function is inaccessible.

无论如何,我可以让Spring使用我的新"api"方法吗?我能想到的唯一选择是在web.xml中创建一个新的Java servlet,但我想做些更简单的事情.我确实尝试用@Controller@Controller("api")制作两个不同的控制器,但这并不能解决问题.

Is there anyway that I can get Spring to use my new "api" method? The only option I can think of is to create a new Java servlet in my web.xml, but I'd like to do something simpler. I did try making two different controllers, with @Controller and @Controller("api"), but that did not solve the problem.

推荐答案

假定user的参数为String,Spring假设已为用户api调用了您.

Assuming that your parameter for user is String, Spring is assuming that you've been invoked for user api.

有两种方法可以解决此问题.第一个(也是最好的)是使您的请求映射更窄:完全指定映射,而不是最后依赖"**".

There are a couple of ways to solve this. The first (and best) is to make your request mappings more narrow: fully specify the mapping, rather than relying on "**" at the end.

第二种方法是使用

The second approach is to disallow an "api" user, using a regex in your mapping. Something like the following should work:

@RequestMapping(value = "{user:[^a][^p][^i]}/**", method = RequestMethod.GET)

这篇关于如何更正Spring MVC RequestMapping顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆