gpt4 book ai didi

java - 如何定义 RequestMapping 优先级

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:14:22 25 4
gpt4 key购买 nike

我有一种情况需要以下RequestMapping:

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

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

有明显的冲突。我希望 Spring 会自动解决这个问题,并将 /support 映射到第二个方法,将其他所有内容映射到第一个方法,但它将 /support 映射到第一个方法。

我如何告诉 Spring 允许显式 RequestMapping 在同一位置用 PathVariable 覆盖 RequestMapping

编辑 2: 如果 /support 映射出现在 /{section} 映射之前,它似乎会起作用。不幸的是,我们有几十个 Controller ,其中包含大量带有 RequestMapping 的方法。如何确保具有 /{section} 映射的 Controller 最后初始化?或者预拦截器是可行的方法吗?

编辑 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}"})

这不会干扰您预先存在的任何 Controller 映射。

关于java - 如何定义 RequestMapping 优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12647346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com