Legend has it that this is the proper way of performing validation on a RestEasy/JAX-RS
endpoint when there is no nested object:
@Value
public class PayloadWithNotNull {
@NotNull Long mSomeLong; // Long is an object, but it's still "Just a long"
}
...
@POST
@Path("test1")
@Produces(MediaType.APPLICATION_JSON)
public Response test1(
final @Valid @NotNull PayloadWithNotNull payload
) {
return ok();
}
Notice how parameter payload
has both @Valid
and @NotNull
, but mSomeLong
only has @NotNull
.
Here is what I observe depending on the request’s json body :
- Sending
null
-> 400 with explicit message “test1.payload must not be null” - Sending
{}
-> 400 without an explicit message! - Sending
{ "someLong": null }
-> 400 without an explicit message! - Sending
{ "someLong": "not a long" }
-> 400 without an explicit message!
However, if I add @Valid on the class field:
@Value
public class PayloadWithValidNotNull {
@Valid @NotNull Long mSomeLong;
}
- Sending
null
-> 400 with explicit message “test1.payload must not be null” - Sending
{}
-> 400 with explicit message “test1.payload.someLong must not be null” - Sending
{ "someLong": null }
-> 400 with explicit message “test1.payload.someLong must not be null” - Sending
{ "someLong": "not a long" }
-> 400 without an explicit message! (that’s lame but at least the behaviour is consistent)
I was under the impression that @Valid is only required for nested objects. I seem to read that everywhere. And yet, it seems absolutely required for this Long field, which despite being itself an object does not have nested objects.
Am I missing something?
Bonus question : is there a simple way to get an explicit error message for “not a long” ?