0

I'm developing a REST API with Google App Engine JAVA with Jersey and JAX-RS. I want to be able to send custom errors to users in JSON format, for that I'm using javax.ws.rs.ext.ExceptionMapper

All works well when I run the app with Jetty on my local machine, but when I deploy to Google I get the default HTML 404 page

Here is the resource code:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}")
public Customer getCustomer(@PathParam("id") String id) {
    Customer customer = getCustomer(id);
    if(customer == null)
        throw new NotFoundException("Customer not found");
    return customer;
}

The exception mapper:

@Provider
public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> {

@Override
public Response toResponse(NotFoundException e) {
    ErrorMessage errorMessage = new ErrorMessage();
    errorMessage.setErrrorMessage(e.getMessage());
    errorMessage.setResponseCode(404);
    return Response.status(Response.Status.NOT_FOUND).entity(errorMessage).type(MediaType.APPLICATION_JSON_TYPE).build();
}    
}

I expect to get the JSON formatted ErrorMessage object as response, but all I get is the default HTML 404 page.

Heigo
  • 936
  • 8
  • 18
  • Can you specify/paste the server error stack? whether your code throws NotFoundException? – Vijay Jun 07 '16 at 16:14
  • Well Jersey catches the NotFoundException and maps it to HTTP 404 responce. So there is no stack – Heigo Jun 07 '16 at 16:16
  • I hope this link will help you http://stackoverflow.com/questions/26903729/how-to-handle-service-unavailable-scenarios-with-jersey-rest – Rakesh Chouhan Jun 07 '16 at 16:21
  • Not really, I am able to modify the HTML page I get when 404 occurs. But I want the response to be JSON formatted string – Heigo Jun 07 '16 at 16:33
  • Can you log statements to confirm that the request is (a) actually invoking getCustomer(), and (b) invoking ExceptionMapper.toresponse()? – Nacho Coloma Jun 07 '16 at 17:18
  • 1
    Try and set the property [`ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR`](https://jersey.java.net/apidocs/2.22/jersey/org/glassfish/jersey/server/ServerProperties.html#RESPONSE_SET_STATUS_OVER_SEND_ERROR) to true – Paul Samsotha Jun 08 '16 at 00:03
  • Yes, thank you! @peeskillet This was it – Heigo Jun 08 '16 at 08:29

1 Answers1

1

You can set the Jersey property ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR to true.

Whenever response status is 4xx or 5xx it is possible to choose between sendError or setStatus on container specific Response implementation. E.g. on servlet container Jersey can call HttpServletResponse.setStatus(...) or HttpServletResponse.sendError(...).

Calling sendError(...) method usually resets entity, response headers and provide error page for specified status code (e.g. servlet error-page configuration). However if you want to post-process response (e.g. by servlet filter) the only way to do it is calling setStatus(...) on container Response object.

If property value is true the method Response.setStatus(...) is used over default Response.sendError(...).

Type of the property value is boolean. The default value is false.

The name of the configuration property is "jersey.config.server.response.setStatusOverSendError".

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720