泽西岛-为资源方法注册ExceptionMapper [英] Jersey - Register ExceptionMapper for Resource Methods

查看:104
本文介绍了泽西岛-为资源方法注册ExceptionMapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题特定类的异常处理/映射使我想到了如何将ExceptionMapper注册到特定资源方法.

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.

我试图使用DynamicFeature这样:

@Provider
public class ExceptionMapperDynamicFeature implements DynamicFeature {

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

}

注释

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

资源

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

结果令人失望:

警告:类... WebApplicationExceptionMapper提供程序的给定协定(接口javax.ws.rs.ext.ExceptionMapper)不能绑定到资源方法.

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

然后,我一直在寻找其他没有运气的可能性,并最终获得了AspectJ实现,您可以将其作为

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.

那么完整的问题:

是否可以将ExceptionMapper成功注册到资源方法?

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

当然

  • 如果是,那么:如何?
  • 如果没有,为什么:

我很好奇答案:)

请注意:
这个问题不会将ExceptionMapper注册到资源 Class ,例如...

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.

推荐答案

并不是您所要的完全是 ,但这是我发现要做自己想要的事情的唯一方法:

It's not exactly what you asked, but it's the only way I've found to do something like you want:

@Provider
public class ErrorExceptionHandler implements ExceptionMapper<Throwable> {

    @Context
    private ResourceInfo resourceInfo;

    @Override
    public Response toResponse(final Throwable exception) {
        // this is an example of how to do something custom based on an annotation
        if(!resourceInfo.getResourceClass().isAnnotationPresent(SomeCustomAnnotation.class) && !resourceInfo.getResourceMethod().isAnnotationPresent(SomeCustomAnnotation.class))
            return null; // do some custom thing here based on your annotation, maybe call another ExceptionMapper not annotated with @Provider ?
        return Response.status(500).entity("ERROR").build();
    }
}

它仍然可以满足我的目的.

It works for my purposes anyway.

这篇关于泽西岛-为资源方法注册ExceptionMapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆