خرید بک لینک

Vote count: 0

The question Exception Handling/Mapping for a particular class brought me to the question of how to register an ExceptionMapper to a particular resource Method.

I've tried to use a DynamicFeature like this:

DynamicFeature

@Provider
public class ExceptionMapperDynamicFeature implements DynamicFeature {

    @Override
    public void configure(final ResourceInfo resourceInfo, final FeatureContext context) {
        if(!resourceInfo.getResourceMethod().isAotationPresent(BindExceptionMapper.class))
            retu;
        BindExceptionMapper bem = resourceInfo.getResourceMethod().getAotation(BindExceptionMapper.class);
        context.register(bem.mapper());
    }

}

Aotation

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface BindExceptionMapper {
    Class<? extends ExceptionMapper<?>> mapper() default WebApplicationExceptionMapper.class;
}

Resource

@GET
@Produces(MediaType.APPLICATION_JSON)
@BindExceptionMapper 
public Response test() {
    // do something that throws an exception
}

The result was disillusioning:

WARNING: The given contract (interface javax.ws.rs.ext.ExceptionMapper) of class ...WebApplicationExceptionMapper provider caot be bound to a resource method.

Then I was searching for other possibilities without luck and ended up with a AspectJ implementation, which you can see as a part of my answer to the linked question above.

So the complete question:

Is there a way to successfully register an ExceptionMapper to a resource Method?

And of course,

  • if yes then: how?
  • if no then: why?

I'm curious about the answers :)

Please notice:
This question is not about to register an ExceptionMapper to a resource Class like...

public class ApplicationResourceConfig extends ResourceConfig {
    public ApplicationResourceConfig() {
        register(WebApplicationExceptionMapper.class, TestResource.class);
    }
}

and especially not about to register them at all.

asked 4 mins ago

برچسب: نویسنده: استخدام کار تاريخ: شنبه 14 فروردين 1395 ساعت: 23:17

صفحه بندی