Is it in some way possible that an exception thrown from a rest service is returned as JSON? I have a JAX-RS Rest Service where I would like to achieve this. When I throw it now, it's mapped to an HTML response, which is not what i want. From what I have understood an ExceptionMapper will also map it to HTML? Is there any other alternative or libraries that allows the exception to be returned in JSON format?
4 Answers
It will respond as JSON.
@Provider @Singleton public class ExceptionMapperProvider implements ExceptionMapper<Exception> { @Override public Response toResponse(final Exception exception) { return Response.status(HttpStatusCodes.STATUS_CODE_SERVER_ERROR).entity(new BasicResponse(InternalStatus.UNHANDLED_EXCEPTION, exception.getMessage())).type(MediaType.APPLICATION_JSON).build(); } } @XmlRootElement public class BasicResponse { public String internalStatus; public String message; public BasicResponse() {} public BasicResponse(String internalStatus, String message){ this.internalStatus = internalStatus; this.message = message; } } 4 Comments
user16655
Thank you. The problem is that the REST service returns f.ex. a list of objects for a GET request. How do I handle this, when what needs to be returned for an error is a Response object?
Justinas Jakavonis
I don't understand your question, please give more details. ExceptionMapper should catch any Exception type in service and return Response with Http status and Json error message.
user16655
What I meant is that my Rest service returns lists of custom objects, whilst this error handling returns a Response object which doesn't comply with my method signature. I guess I need to alter so that I return a Response object instead. This can be a bit problematic in my POST methods on the other hand, which now are void. Hope that made it clearer :)
Justinas Jakavonis
If exceptional situation occurs, you won't return common response with custom objects or void. You need to return error with specific message. So you may left method signature as it is but exception mapper will return Response type. Also you can specify how to handle specific exception type in the exception mapper.
You can create custom exception,It takes JSON request and response
@POST @Path("/betRequest") @Consumes({ "application/json", "application/x-www-form-urlencoded" }) @Produces({ "application/json", "application/x-www-form-urlencoded" }) public Response getBetRequest(String betRequestParams, @Context HttpServletRequest request) { BetResponseDetails betResponseDetails = new BetResponseDetails(); try{ //you code here } catch (JSONException ex) { ex.printStackTrace(); betResponseDetails.setResponseCode("9002");//your custom error code betResponseDetails.setResponseStatus("Bad Request");//custom status betResponseDetails.setResponseMessage("The request body contained invalid JSON");//custom error massage return Response.status(200).entity(betResponseDetails).build(); } } Create One POJO BetResponseDetails
public class BetResponseDetails { private String ResponseStatus; private String ResponseCode; private String ResponseMessage; // getter/setter ....... }
@ResponseStatus(value="HttpStatus.XXXXXXXXX",reason"blah blah") @ExceptionHandler(MyExceptionException.class) ..