How to validate the size of @RequestParam of type List

前端 未结 3 1043
走了就别回头了
走了就别回头了 2021-01-13 21:53

I\'m creating a Spring-Boot microservice REST API that expects @RequestParam of type List. How can I validate that the list contains

3条回答
  •  逝去的感伤
    2021-01-13 22:32

    You can use a Class and @RequestBody for parameter validation, which is successful like me.

    public class Test {
        @Size(min = 1 , max = 5)
        private List programEndTime;
    
        public List getProgramEndTime() {
            return programEndTime;
        }
    
        public void setProgramEndTime(List programEndTime) {
            this.programEndTime = programEndTime;
        }
    }
    
        @PostMapping("test")
        public void test( @Valid   @RequestBody Test test,
                         BindingResult result){
            if (result.hasErrors()) {
                System.out.println("Error");
            } else {
                System.out.println("OK");
            }
            System.out.println(",.,.");
        }
    

提交回复
热议问题