I'm having trouble getting ValidationErrors from a jersey resource in jersey client. Let me explain.
A function in jersey resource:
@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes(MediaType.APPLICATION_JSON)
public LoginInfo login(@NotNull @Valid Login login)
My clientconfig:
ClientConfig clientConfig = new ClientConfig();
clientConfig.register(JacksonFeature.class);
My jersey client:
ClientBuilder.newClient(getClientConfig())
And my function call:
getTarget("/login").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(login), LoginInfo.class);
All works without problems as long i supply valid parameters, when i for example supply null as parameter i get exception:
HTTP 400 Bad Request
But i'm expecting a ValidationError response, because bean validation failed.
Bean validation and error response is working on server, for example with a simple html test, which shows validationerror structure:
<div class="validation-errors"><div class="validation-error"><span class="message">may not be null</span> (<span class="path"><strong>path</strong> = LoginResource.login.arg0</span>, <span class="invalid-value"><strong>invalidValue</strong> = null</span>)</div></div>
How do i get the ValidationError in my jersey client? Do i maybe have to configure it in a special way or maybe i should use a filter?
[edit] I turned on tracing and when validation fails on server the server sends validationerrors, but it seems jersey client doesn't do anything with it, it converts it to BadRequestException.
6 < 400
6 < Content-Type: application/json
6 < Vary: Accept
[{"message":"may not be empty","messageTemplate":{org.hibernate.validator.constraints.NotBlank.message}","path":"RuleResource.add.arg0.description","invalidValue":""}]
Answering my own question ;-)
It seems jersey client doesn't support validationerrors, so i created a client filter and when status is 400 i get the entity and put the validation errors in a list. Next i create a validationexception which includes the validation errors and just throw it. A bit further up the chain i get a processingexception, in the stacktrace the validationexception can be found and also the validation errors. So i create a nice message from the validation errors and throw another exception, all the way up to my gui code which simply displays the exception message.
Works fine, thanks jersey for the flexibility of filters!