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.
NotBlank
is a combination of both functionalities that NotNull
and NotEmpty
validate. Therefore @NotBlank
will fire both when the bound pathVariable is null
and when empty
.
Since you already have @Size(min = 1, max = 3, message = "Invalid input size")
you are already checking if not empty
. This annotation will fire at the same time with @NotBlank
in case the pathVariable is empty
string.
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 @NotBlank
with @NotNull
.
@PathVariable(name = "id")
@NotNull(message = "Missing required field")
@Size(min = 1, max = 3, message = "Invalid input size")
String id