Unless there is some more subtle problem at play here, most likely the 
issue is with the approach you're using to read the response body (i.e., 
using a high-level client layer that throws an exception if it doesn't get 
a successful status code and/or the content-type it expected).  Certainly 
there is nothing in the Dropwizard framework that prevents sending a JSON 
entity body or other content on non-2xx responses... for example, I usually 
like to tell clients what's wrong with their malformed JSON as part of 400 
Bad Request responses.  How does it look if you test with cURL?


On Saturday, May 20, 2017 at 8:03:59 PM UTC-4, John Reece wrote:
>
> 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.

Reply via email to