Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering a provider programmatically in jersey which implements exceptionmapper

How do I register my provider programmatically in jersey which implements the Exceptionmapper provided by jersey API? I don't want to use @Provider annotation and want to register the provider using ResourceConfig, how can I do that?

For example:

public class MyProvider implements ExceptionMapper<WebApplicationException> extends ResourceConfig {

     public MyProvider() {
        final Resource.Builder resourceBuilder = Resource.builder();
        resourceBuilder.path("helloworld");

        final ResourceMethod.Builder methodBuilder = resourceBuilder.addMethod("GET");
        methodBuilder.produces(MediaType.TEXT_PLAIN_TYPE)
                .handledBy(new Inflector<ContainerRequestContext, String>() {

            @Override
            public String apply(ContainerRequestContext containerRequestContext) {
                return "Hello World!";
            }
        });

        final Resource resource = resourceBuilder.build();
        registerResources(resource);
    }

    @Override
    public Response toResponse(WebApplicationException ex) {
        String trace = Exceptions.getStackTraceAsString(ex);
        return Response.status(500).entity(trace).type("text/plain").build();
    }
}

Is this the correct way to do this?

like image 858
Gautam Avatar asked Jul 19 '15 12:07

Gautam


2 Answers

I'm guessing you don't have a ResourceConfig, since you seem to not be sure how to use it. For one, it is not required. If you do use it, it should be it's own separate class. There you can register the mapper.

public class AppConfig extends ResourceConfig {
    public AppConfig() {
        register(new MyProvider());
    }
}

But you are probably using a web.xml. In which case, you can register the provider, with the following <init-param>

<servlet>
    <servlet-name>MyApplication</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>
            org.foo.providers.MyProvider
        </param-value>
    </init-param>
</servlet>

Have a look at What exactly is the ResourceConfig class in Jersey 2? for more information on different deployment models. There are a few different ways to deploy applications. You can even mix and match (web.xml and ResourceConfig).

like image 131
Paul Samsotha Avatar answered Oct 20 '22 10:10

Paul Samsotha


While @paul-samsotha's answer is correct, still there is implementation trick. I want to share it and hope it will help someone.

a) Implement your mapper:

public class MyExceptionMapper implements ExceptionMapper<Throwable>, ResponseErrorMapper {
    ...

b) make sure you declare generic type, otherwise your mapper will never be called

public class MyExceptionMapper implements ExceptionMapper/* no generic declaration */, ResponseErrorMapper {
    ...

and may trigger

javax.ws.rs.ProcessingException: Could not find exception type for given ExceptionMapper class: class com...MyExceptionMapper.

c) Register it as resource:

ResourceConfig config = new ResourceConfig();
config.register(new MyExceptionMapper());

or

config.register(MyExceptionMapper.class);

d) make sure you enforce processing errors as well:

config.setProperties(new LinkedHashMap<String, Object>() {{
    put(org.glassfish.jersey.server.ServerProperties.PROCESSING_RESPONSE_ERRORS_ENABLED, true);
}});
like image 1
ursa Avatar answered Oct 20 '22 10:10

ursa