Home » Java » java – How do I register ExceptionMapper with JerseyClientBuilder?-Exceptionshub

java – How do I register ExceptionMapper with JerseyClientBuilder?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am working on REST web service which uses Jersey.
I am trying to register exception mappers with JerseyClientBuilder:

JerseyClientBuilder().property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true)
                                  .register(NotFoundException.class)
                                  .build();

Where NotFoundException is

public class NotFoundException extends RuntimeException implements ExceptionMapper<NotFoundException> {

    private static final long serialVersionUID = 1L;

    public NotFoundException(final String message) {
        super(message);
    }

    @Override
    public Response toResponse(final NotFoundException exception) {
        return Response.status(404).entity(exception.getMessage())
                       .type("text/plain").build();
    }
}

but it doesn’t work, i.er. exception is not mapped. What I am doing wrong? How can I map it correctly?

How to&Answers: