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
It will respond as JSON.
@Provider
@Singleton
public class ExceptionMapperProvider implements ExceptionMapper
{
@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;
}
}