Annotations from javax.validation.constraints not working

后端 未结 15 1347
广开言路
广开言路 2020-11-28 06:23

What configuration is needed to use annotations from javax.validation.constraints like @Size, @NotNull, etc.? Here\'s my code:

15条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 06:35

    I come here some years after, and I could fix it thanks to atrain's comment above. In my case, I was missing @Valid in the API that receives the Object (a POJO in my case) that was annotated with @Size. It solved the issue.

    I did not need to add any extra annotation, such as @Valid or @NotBlank to the variable annotated with @Size, just that constraint in the variable and what I mentioned in the API...

    Pojo Class:

    ...
    @Size(min = MIN_LENGTH, max = MAX_LENGTH);
    private String exampleVar;
    ...
    

    API Class:

    ...
    public void exampleApiCall(@RequestBody @Valid PojoObject pojoObject){
      ...
    }
    

    Thanks and cheers

提交回复
热议问题