正则表达式错误,改用非捕获组
Regex error, use non-capturing groups instead
我有一个 spring 请求映射方法,我在其中为路径变量定义了正则表达式,但是 Spring 在测试此方法时抛出异常,我应该更改什么?
@GetMapping("/items/item/{name:[a-zA-Z]+(\s[a-zA-Z]+)*}")
public Item getItemByName(@PathVariable String name) {
return itemService.getItemByName(name);
}
错误:
org.springframework.web.util.NestedServletException: Request
processing failed; nested exception is
java.lang.IllegalArgumentException: The number of capturing groups in
the pattern segment ([a-zA-Z]+(\s[a-zA-Z]+)*) does not match the
number of URI template variables it defines, which can occur if
capturing groups are used in a URI template regex. Use non-capturing
groups instead.
从堆栈跟踪来看,您似乎应该使用非捕获组。
使用[a-zA-Z]+(?:\s[a-zA-Z]+)*
代替[a-zA-Z]+(\s[a-zA-Z]+)*
If you do not need the group to capture its match, you can optimize this regular expression into Set(?:Value)?.
更多详情here
我有一个 spring 请求映射方法,我在其中为路径变量定义了正则表达式,但是 Spring 在测试此方法时抛出异常,我应该更改什么?
@GetMapping("/items/item/{name:[a-zA-Z]+(\s[a-zA-Z]+)*}")
public Item getItemByName(@PathVariable String name) {
return itemService.getItemByName(name);
}
错误:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: The number of capturing groups in the pattern segment ([a-zA-Z]+(\s[a-zA-Z]+)*) does not match the number of URI template variables it defines, which can occur if capturing groups are used in a URI template regex. Use non-capturing groups instead.
从堆栈跟踪来看,您似乎应该使用非捕获组。
使用[a-zA-Z]+(?:\s[a-zA-Z]+)*
代替[a-zA-Z]+(\s[a-zA-Z]+)*
If you do not need the group to capture its match, you can optimize this regular expression into Set(?:Value)?.
更多详情here