jax rs BadRequestException("message") gives generic message

1.3k views Asked by At

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.

1

There are 1 answers

0
user3130012 On BEST ANSWER

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();
  }
}