繁体   English   中英

如何在Spring MVC中定义请求映射的优先级?

[英]How to define priorities for request mappings in Spring MVC?

使用SpringMVC,我有一个捕获所有REST请求的方法:

@RequestMapping(value = "/**")
public Object catchAll(@RequestBody(required = false) Object body, HttpMethod method, HttpServletRequest request, HttpServletResponse response) {
    // ...
}

现在,我想用以下端点捕获一些请求:

@RequestMapping(value = "/test", method = RequestMethod.POST)
public Object post(@RequestBody Object body, HttpMethod method, HttpServletRequest request, HttpServletResponse response) {
    // ...
}

现在,当我打电话给:

/测试

从不调用第二种方法。

要使我的第二个方法始终代替第一个方法,该怎么办?

首先,正如Asura所指出的,不要实现'catchAll'方法。 话虽如此,Spring MVC允许您在URL中使用正则表达式。

此处阅读在Spring URL中使用正则表达式的文档。

在您的情况下,请在第一个URL中使用否定前瞻,以删除以/test开头的内容。 这样,您的/**控制器将捕获除以/test开头的请求之外的所有请求,您可以使用其他控制器来捕获这些请求。

在第一个网址中使用否定的超前查询:

@RequestMapping(value = "/^(?!test)")
public Object catchAll(@RequestBody(required = false) Object body, HttpMethod method, HttpServletRequest request, HttpServletResponse response) {
// ...

}

现在,这应该捕获/test请求:

@RequestMapping(value = "/test", method = RequestMethod.POST)
public Object post(@RequestBody Object body, HttpMethod method, HttpServletRequest request, HttpServletResponse response) {
// ...

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM