两个 Spring 控制器匹配到相同的 URL 导致错误的控制器调用
Two Spring Controllers matching to the same URL result in wrong controller call
我们遇到了两个 spring 控制器碰巧相互干扰的问题。
一个控制器通过通配符前缀匹配 URL 后缀。另一个控制器匹配 URL 前缀。我希望 URLs 是从左到右阅读的,但似乎并非如此。
考虑以下代码(已编辑):
@RequestMapping(value = "/**/abcdefg")
public class Controller1 {...}
@RequestMapping(value = "/**/xyz")
public class Controller2 {...}
@RequestMapping(value = "/some/{path}")
public class Controller3
{
@RequestMapping(value = "/{page}", method = RequestMethod.GET)
public String page(@PathVariable("page") final String page, final Model model)
{ //do sth }
}
现在的问题是,如果调用 URL“/some/path/abcdefg”,Controller1 就会启动。但我想要 Controller3。
不幸的是,这种行为与其他控制器不同!
如果调用 URL“/some/path/xyz”,Controller3 将启动。此行为对于控制器是可重现的。它的行为始终相同,并且控制器不是随机选择的。
spring 文档以及其他用户提出的问题都指出了这样的想法,即 "first" 控制器被采用,与给定的模式相匹配。但是,这对我来说意义不大,因为 Controller1 和 2 有一个非常 相似的请求映射,但控制器匹配不同!
一切都由调度程序 servlet 处理。
有没有人暗示可能会发生什么?
这部分 我希望从左到右阅读 URL,但事实并非如此。 你是对的,事实并非如此。映射由特异性解析,相关文档可用 here
A pattern with a lower count of URI variables and wild cards is
considered more specific. For example /hotels/{hotel}/*
has 1 URI
variable and 1 wild card and is considered more specific than
/hotels/{hotel}/**
which as 1 URI variable and 2 wild cards.
If two patterns have the same count, the one that is longer is
considered more specific. For example /foo/bar*
is longer and
considered more specific than /foo/*
.
When two patterns have the same count and length, the pattern with
fewer wild cards is considered more specific. For example
/hotels/{hotel}
is more specific than /hotels/*
.
如果两个映射匹配一个请求,将应用更具体的。根据您的要求制定您的映射以遵循特定规则
我们遇到了两个 spring 控制器碰巧相互干扰的问题。 一个控制器通过通配符前缀匹配 URL 后缀。另一个控制器匹配 URL 前缀。我希望 URLs 是从左到右阅读的,但似乎并非如此。
考虑以下代码(已编辑):
@RequestMapping(value = "/**/abcdefg")
public class Controller1 {...}
@RequestMapping(value = "/**/xyz")
public class Controller2 {...}
@RequestMapping(value = "/some/{path}")
public class Controller3
{
@RequestMapping(value = "/{page}", method = RequestMethod.GET)
public String page(@PathVariable("page") final String page, final Model model)
{ //do sth }
}
现在的问题是,如果调用 URL“/some/path/abcdefg”,Controller1 就会启动。但我想要 Controller3。
不幸的是,这种行为与其他控制器不同!
如果调用 URL“/some/path/xyz”,Controller3 将启动。此行为对于控制器是可重现的。它的行为始终相同,并且控制器不是随机选择的。
spring 文档以及其他用户提出的问题都指出了这样的想法,即 "first" 控制器被采用,与给定的模式相匹配。但是,这对我来说意义不大,因为 Controller1 和 2 有一个非常 相似的请求映射,但控制器匹配不同!
一切都由调度程序 servlet 处理。
有没有人暗示可能会发生什么?
这部分 我希望从左到右阅读 URL,但事实并非如此。 你是对的,事实并非如此。映射由特异性解析,相关文档可用 here
A pattern with a lower count of URI variables and wild cards is considered more specific. For example
/hotels/{hotel}/*
has 1 URI variable and 1 wild card and is considered more specific than/hotels/{hotel}/**
which as 1 URI variable and 2 wild cards.If two patterns have the same count, the one that is longer is considered more specific. For example
/foo/bar*
is longer and considered more specific than/foo/*
.When two patterns have the same count and length, the pattern with fewer wild cards is considered more specific. For example
/hotels/{hotel}
is more specific than/hotels/*
.
如果两个映射匹配一个请求,将应用更具体的。根据您的要求制定您的映射以遵循特定规则