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?
Asked
Active
Viewed 6,101 times
4 Answers
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;
}
}

Justinas Jakavonis
- 8,220
- 10
- 69
- 114
-
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? – user16655 Apr 07 '16 at 08:21
-
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. – Justinas Jakavonis Apr 07 '16 at 08:34
-
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 :) – user16655 Apr 07 '16 at 08:38
-
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. – Justinas Jakavonis Apr 07 '16 at 08:48
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
.......
}

vaibhav patil
- 143
- 11
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"
}
}

dneranjan
- 136
- 1
- 2
- 14
-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)

Ji aSH
- 3,206
- 1
- 10
- 18