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.
NotBlankis a combination of both functionalities thatNotNullandNotEmptyvalidate. Therefore@NotBlankwill fire both when the bound pathVariable isnulland whenempty.Since you already have
@Size(min = 1, max = 3, message = "Invalid input size")you are already checking ifnot empty. This annotation will fire at the same time with@NotBlankin case the pathVariable isemptystring.So you only need a different constraint validation for when it is
null, so that the 2 annotations that enforce constraints do not override each other.As a solution you can replace
@NotBlankwith@NotNull.