I'm building REST/JSON services for which there is a defined JSON response
payload for *every* HTTP-result-code, including non-2xx codes.
The service code reports errors by throwing an exception that contains
the javax.ws.rs.core.Response.StatusType as well as a custom object
containing detailed error messaging. The custom object should be serialized
to JSON and returned in the response.
This all works fine *except* that when a non-2xx HTTP-result-code is used,
no JSON is written to the response.
I have registered a custom exception mapper, and verified that it works as
intended, but I cannot figure out how to get the JSON response returned.
If I change the value passed to ResponseBuilder.status() to any 2xx code
(eg. Status.OK), the entity is returned in the response, properly
serialized to JSON. If I pass any status value other than those for 2xx
codes, no entity is returned in the response.
My exception mapper looks like this:
@Provider
public class ApiExceptionMapper implements ExceptionMapper<ApiException> {
@Override
public Response toResponse(final ApiException exception) {
// get the error response details from the exception
final ApiErrorInfo apiErrorInfo = new ApiErrorInfo(exception);
// build the response
final ResponseBuilder builder = Response
.status(apiErrorInfo.getStatus())
.entity(apiErrorInfo.getResponse())
.type(MediaType.APPLICATION_JSON);
return builder.build();
}
}
And in my Application#run method, I have this:
((AbstractServerFactory) configuration.getServerFactory())
.setRegisterDefaultExceptionMappers(false);
environment.jersey().register(new ApiExceptionMapper());
What am I missing ?
--
You received this message because you are subscribed to the Google Groups
"dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.