I am applying multiple validations on path variable
@PathVariable(name = "id")
@NotBlank(message = "Missing required field")
@Size(min = 1, max = 3, message = "Invalid input size")
String id
Now, when I am sending empty string in path param then I am getting both messages because both validations are failing.
For my param id, I want both validations but it should not throw both error messages at a time when I am sending empty string.
I want it to throw only
"Missing required field"
and not both.
1条答案
按热度按时间wgeznvg71#
NotBlank
是NotNull
和NotEmpty
验证的两个功能的组合。因此,当绑定的pathVariable为null
和empty
时,@NotBlank
都将触发。因为您已经有
@Size(min = 1, max = 3, message = "Invalid input size")
,所以您已经在检查not empty
。如果pathVariable是empty
字串,这个注解会与@NotBlank
同时触发。因此,当它是
null
时,您只需要一个不同的约束验证,这样强制约束的两个注解就不会相互覆盖。作为解决方案,您可以将
@NotBlank
替换为@NotNull
。