草庐IT

java - 无法映射正则表达式 - java.lang.IllegalArgumentException : The number of capturing groups in the pattern segment

coder 2024-03-06 原文

我在我的 Controller 中定义了以下方法:

@RequestMapping(value = "/ajax/comments/post/{contentId:([apv]|ad)\\d+}")
    public @ResponseBody
    ActionResult handlePostCommentRequest(HttpServletRequest request, Model model,
            @PathVariable("contentId") String assetId,
            @RequestParam(value = "nickName", required = false, defaultValue = "Anonyymi") String nickName,
            @RequestParam(value = "text", required = false, defaultValue = "") String text,
            @RequestParam(value = "createThread", required = false, defaultValue = "false") String createThread) {
            // some code...
}

但是,当我执行以下 HTTP 请求时 -/ajax/comments/post/ad1374659405664 我得到异常:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: The number of capturing groups in the pattern segment (([apv]|ad)\d+) 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.

Google 没有给出那么多结果,这很奇怪,因为当我检查正则表达式时 ([vpa]|广告)\d+http://regexpal.com/它正确匹配所有内容。我做错了什么?

最佳答案

错误消息中已说明一切:改用非捕获组

(?:[apv]|ad)\\d+

参见 http://www.regular-expressions.info/brackets.html了解更多详情。

关于java - 无法映射正则表达式 - java.lang.IllegalArgumentException : The number of capturing groups in the pattern segment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20323848/

有关java - 无法映射正则表达式 - java.lang.IllegalArgumentException : The number of capturing groups in the pattern segment的更多相关文章

随机推荐