如何定义RequestMapping优先级 [英] How to define RequestMapping prioritization
问题描述
我的情况是需要以下 RequestMapping
:
@RequestMapping(value={"/{section}"})
...method implementation here...
@RequestMapping(value={"/support"})
...method implementation here...
存在明显的冲突。我希望Spring会自动解决这个问题,并将 / support
映射到第二个方法,并将其他所有内容映射到第一个方法,但它会映射 / support
到第一种方法。
There is an obvious conflict. My hope was that Spring would resolve this automatically and map /support
to the second method, and everything else to the first, but it instead maps /support
to the first method.
如何告诉Spring允许显式 RequestMapping
在同一个地方用 PathVariable
覆盖 RequestMapping
?
How can I tell Spring to allow an explicit RequestMapping
to override a RequestMapping
with a PathVariable
in the same place?
编辑2:如果 / support
映射位于 / {section}之前,它似乎可以正常工作
映射。不幸的是,我们有许多控制器包含 RequestMapping
的众多方法。如何确保最后初始化具有 / {section}
映射的控制器?或者预拦截器是否可行?
Edit 2: It seems that it would work if the /support
mapping came before the /{section}
mapping. Unfortunately we have dozens of controllers containing numerous methods with RequestMapping
. How can I make sure that the controller with the /{section}
mapping is initialized last? Or would a pre-interceptor be the way to go?
编辑1:这是简化的,我知道有两个 RequestMapping
单独没有多大意义)
Edit 1: This is simplified, I know that having those two RequestMapping
alone wouldn't make much sense)
推荐答案
使用Spring你可以扩展org.springframework.web.HttpRequestHandler以支持您的方案。
Using Spring you can extend the org.springframework.web.HttpRequestHandler to support your scenario.
实施方法:
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
使用它来分析传入的请求,确定请求网址是否是您的特殊请求网址子集的一部分并转发到适当的位置。
Use it to analyze the incoming request, determine if the request url is part of your special subset of request url's and forward to the appropriate location.
例如:
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/** You will want to check your array of values and have this data cached **/
if (urlPath.contains("/sectionName")) {
RequestDispatcher requestDispatcher = request.getRequestDispatcher("sections" + "/" + urlPath);
requestDispatcher.forward(request, response);
}
}
并设置以下部分:
@RequestMapping(value={"/sections/{sectionName}"})
这不会干扰任何预先存在的控制器映射。
This will not interfere with any of your pre-existing controller mappings.
这篇关于如何定义RequestMapping优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!