Reputation: 449
i'm throwing a jax rs BadRequestException("message") and i always get the generic message back and not the one i put in there. why is that? i'm using netbeans and postman chrome extension to test and see the response.
Upvotes: 2
Views: 1595
Reputation: 449
for the BadRequestException("message") to display when you request, you need an extra class that implements ExceptionMapper. learned this after i had learned to create my own custom exception classes. i must of skipped over the part they explained this in my book. anyway here is how it looks like.
import javax.ws.rs.BadRequestException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class BadRequestExceptionMapper implements ExceptionMapper < BadRequestException > {
@Override
public Response toResponse(BadRequestException exception) {
return Response.status(Status.BAD_REQUEST).type(MediaType.TEXT_PLAIN).entity(exception.getMessage()).build();
}
}
Upvotes: 3