如何定义RequestMapping优先级


问题内容

我遇到以下情况RequestMapping

@RequestMapping(value={"/{section}"})
...method implementation here...

@RequestMapping(value={"/support"})
...method implementation here...

有明显的冲突。我希望Spring可以自动解决此问题,并映射/support到第二种方法,其他所有内容都映射/support到第一种方法,但改为映射到第一种方法。

我怎么能告诉Spring允许一个明确的RequestMapping覆盖一个RequestMappingPathVariable在同一个地方?

编辑2:
如果/support映射先于/{section}映射,则似乎可以使用。不幸的是,我们有数十个控制器,其中包含许多方法RequestMapping。如何确保带有/{section}映射的控制器最后被初始化?还是预拦截器是必经之路?

编辑1: 这是简化的,我知道RequestMapping单独拥有这两个没有多大意义)


问题答案:

使用Spring可以扩展org.springframework.web.HttpRequestHandler以支持您的方案。

实现方法:

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}

使用它来分析传入的请求,确定请求url是否是请求url的特殊子集的一部分,然后转发到适当的位置。

例如:

@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}"})

这不会干扰您之前存在的任何控制器映射。