JAX-RS (Jersey) custom exception with XML or JSON

I have a REST service built using Jersey.

I want to be able to set the MIME of my own exception creators based on the MIME that was sent to the server. application/json returned when receiving json and application/xml when receiving xml.

Now I have hardcoded application/json code, but this makes the XML clients left in the dark.

 public class MyCustomException extends WebApplicationException { public MyCustomException(Status status, String message, String reason, int errorCode) { super(Response.status(status). entity(new ErrorResponseConverter(message, reason, errorCode)). type("application/json").build()); } } 

In what context can I access current Content-Type requests?

Thank!




Response Based Update

For anyone interested in a complete solution:

 public class MyCustomException extends RuntimeException { private String reason; private Status status; private int errorCode; public MyCustomException(String message, String reason, Status status, int errorCode) { super(message); this.reason = reason; this.status = status; this.errorCode = errorCode; } //Getters and setters } 

Together with ExceptionMapper

 @Provider public class MyCustomExceptionMapper implements ExceptionMapper<MyCustomException> { @Context private HttpHeaders headers; public Response toResponse(MyCustomException e) { return Response.status(e.getStatus()). entity(new ErrorResponseConverter(e.getMessage(), e.getReason(), e.getErrorCode())). type(headers.getMediaType()). build(); } } 

Where ErrorResponseConverter is a custom JAXB POJO

+43
java jersey mime jax-rs
Jul 12 2018-10-12T00:
source share
2 answers

You can try adding the @ javax.ws.rs.core.Context javax.ws.rs.core.HttpHeaders field / property to your root resource class, resource method parameter, or custom javax.ws.rs. ext.ExceptionMapper and a call to HttpHeaders.getMediaType ().

+24
Jul 17 '10 at 12:11
source share

headers.getMediaType () responds with an Entity media type, not an Accept header. An appropriate way to throw an exception is the Accept header, so that your client receives the response in the requested format. Given the above solution, if your request looks like this (note the JSON accept header, but XML Entity), you will get the XML back.

 POST http: // localhost: 8080 / service / giftcard / invoice? Draft = true HTTP / 1.1
 Accept: application / json
 Authorization: Basic dXNlcjp1c2Vy
 Content-Type: application / xml
 User-Agent: Jakarta Commons-HttpClient / 3.1
 Host: localhost: 8080
 Proxy-Connection: Keep-Alive
 Content-Length: 502
 <? xml version = "1.0" encoding = "UTF-8" standalone = "yes"?> <sample> <node1> </node1> </sample>

The correct implementation is again used to use the Accept header:

 public Response toResponse(final CustomException e) { LOGGER.debug("Mapping CustomException with status + \"" + e.getStatus() + "\" and message: \"" + e.getMessage() + "\""); ResponseBuilder rb = Response.status(e.getStatus()).entity( new ErrorResponseConverter(e.getMessage(), e.getReason(), e.getErrorCode())); List<MediaType> accepts = headers.getAcceptableMediaTypes(); if (accepts!=null && accepts.size() > 0) { //just pick the first one MediaType m = accepts.get(0); LOGGER.debug("Setting response type to " + m); rb = rb.type(m); } else { //if not specified, use the entity type rb = rb.type(headers.getMediaType()); // set the response type to the entity type. } return rb.build(); } 
+15
May 31 '12 at 13:57
source share



All Articles