gpt4 book ai didi

java - 正则表达式错误,请改用非捕获组

转载 作者:行者123 更新时间:2023-12-02 11:17:25 25 4
gpt4 key购买 nike

我有一个 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

关于java - 正则表达式错误,请改用非捕获组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50173927/

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