【问题标题】:Regex Validation not happening in Spring Boot ControllerSpring Boot Controller 中未发生正则表达式验证
【发布时间】:2019-06-25 15:30:36
【问题描述】:
@RequestMapping(value="apiInventory/apis",method=RequestMethod.GET)
public Map<String,Object> getData(@NotNull @Valid @RequestParam("host") String host,
        @NotNull @Valid @RequestParam("apiGroup") String basePath,
        @RequestParam(value="path",required=false) String path, 
        @RequestParam(value="version",required=false) String version, 
        @Valid @Pattern(regexp="get|post|put|delete|patch|options") @RequestParam(value="httpMethod",required=false) String httpMethod, 
        @RequestParam(value="includeAttributeDefinition",required=false, defaultValue="false") boolean includeAttributeDefinition) throws Exception{

          //My logic
        }

我必须确保 httpMethod 只接受 get、post、put、delete、patch、options 的值

但是,验证没有发生。我期待一个错误,但当我发送一个无效的httpMethod 时没有抛出任何错误。我做错了什么?

【问题讨论】:

    标签: java regex spring spring-boot


    【解决方案1】:

    您需要使用“@Validated”注释您的控制器类,以便验证工作。

    【讨论】:

      【解决方案2】:

      您的正则表达式并没有按照您的想法执行。您已经用方括号 [] 将整个内容括起来,将其转换为由字符 get|posudlachin 组成的字符类。

      尝试删除方括号,使正则表达式为get|post|put|delete|patch|options

      您可能还需要为httpMethod 使用@Valid 注释。

      【讨论】:

        猜你喜欢
        • 2013-11-30
        • 2020-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-27
        • 2019-03-19
        • 2021-05-07
        • 2021-10-06
        相关资源
        最近更新 更多