泽西岛:如何使用自定义`ExceptionMapperFactory` [英] Jersey: How to use a custom `ExceptionMapperFactory`

查看:53
本文介绍了泽西岛:如何使用自定义`ExceptionMapperFactory`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用自定义ExceptionMapperFactory来实现自定义find()逻辑.

I need to use a custom ExceptionMapperFactory to implement a custom find() logic.

public class MyExceptionMapperFactory extends ExceptionMapperFactory {

    // [...]

    @Override
    public <T extends Throwable> ExceptionMapper<T> find(Class<T> type) {
         // [...]
    }
}

我如何使用/注册它?

在我的RestApplication中注册它无效:

Registering it in my RestApplication has no effect:

public class RestApplication extends ResourceConfig {

    public RestApplication() {
        register(JacksonFeature.class);
        register(JacksonXMLProvider.class);

        register(MyExceptionMapperFactory.class);
        register(SomeExceptionMapper.class, 600);
        register(OtherExceptionMapper.class, 550);

        // [...]
     }
}

有什么想法吗? TIA!

Any ideas? TIA!

推荐答案

我从未实现过此功能,因此我不了解所有细微差别,而是简要地查看测试,看来您只需要

I've never implemented this, so I don't know all the nuances, but briefly looking at the source and the tests, it looks like you just need to hook it up to the DI system

register(new AbstractBinder() {
    @Override
    public void configure() {
        bindAsContract(MyExceptionMapperFactory.class)
                .to(ExceptionMappers.class)
                .in(Singleton.class);
    }
})

不确定如何禁用原始版本,但如果仍在使用,则可以尝试

Not sure how to disable the original one, but if it is still being used, you can try to set a rank for your factory so that when it's looked up, yours will take precedence

bindAsContract(MyExceptionMapperFactory.class)
        .to(ExceptionMappers.class)
        .ranked(Integer.MAX_VALUE)
        .in(Singleton.class);


更新

由于排名不起作用,以下内容似乎将删除原始工厂


UPDATE

It seems the following will remove the original factory, since the ranking doesn't work

@Provider
public class ExceptionMapperFactoryFeature implements Feature {

    @Override
    public boolean configure(FeatureContext context) {
        final ServiceLocator locator = ServiceLocatorProvider.getServiceLocator(context);
        final Descriptor<?> descriptor = locator.getBestDescriptor(new Filter() {
            @Override
            public boolean matches(Descriptor d) {
                return d.getImplementation().equals(ExceptionMapperFactory.class.getName());
            } 
        });
        ServiceLocatorUtilities.removeOneDescriptor(locator, descriptor);

        context.register(new AbstractBinder() {
            @Override
            public void configure() {
                bindAsContract(MyExceptionMapperFactory.class)
                        .to(ExceptionMappers.class)
                        .in(Singleton.class);
            }
        });
        return true;
    }
}

这篇关于泽西岛:如何使用自定义`ExceptionMapperFactory`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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