Spring Boot REST @RequestParam not being Validated

前端 未结 3 648
慢半拍i
慢半拍i 2020-12-13 09:58

I have tried a number of examples from the net and cannot get Spring to validate my query string parameter. It doesn\'t seem execute the REGEX / fail.

packag         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 10:35

    You can try this

    @Pattern(regexp="^[0-9]+(,[0-9]+)*$")
    private static final String VALIDATION_REGEX;
    

    (pay attention for the final modifier) or else

     @Pattern()
     private static final String VALIDATION_REGEX = "^[0-9]+(,[0-9]+)*$";
    

    And then remove @Pattern(regexp = VALIDATION_REGEX) from your method and keep only the @Valid annotation:

    public myResonseObject getMyParams(@PathVariable("id") String id, @Valid @RequestParam(value = "myparam", required = true) String myParam) {
    

提交回复
热议问题