1

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?

1
  • try using annotations if you are using spring.. @ResponseStatus(value="HttpStatus.XXXXXXXXX",reason"blah blah") @ExceptionHandler(MyExceptionException.class) .. Commented Apr 7, 2016 at 5:50

4 Answers 4

5

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; } } 
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
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 :)
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.
2

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 ....... } 

Comments

0

get the response data in a structure with status and data, if the status is error, show the correct message. you can try this way

{ "status": "error", "data": { "message": "information of error message" } 

}

Comments

-1

catch your exceptions, then build a response object in a standardized format such as

error: { code: 'XXX', status: HTTPStatus, message: 'my error message' } 

And send it as a response with an error status (from Response.Status, usually 4xx or 5xx)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.