我需要使用自定义ExceptionMapperFactory来实现自定义find()逻辑.
public class MyExceptionMapperFactory extends ExceptionMapperFactory {
// [...]
@Override
public <T extends Throwable> ExceptionMapper<T> find(Class<T> type) {
// [...]
}
}
如何使用/注册?
在我的RestApplication中注册它无效:
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!
解决方法:
我从来没有实现过,所以我不知道所有的细微差别,但是简要地看一下source和tests,看起来您只需要hook it up to the DI system
register(new AbstractBinder() {
@Override
public void configure() {
bindAsContract(MyExceptionMapperFactory.class)
.to(ExceptionMappers.class)
.in(Singleton.class);
}
})
不确定如何禁用原始版本,但如果仍在使用它,则可以尝试将set a rank用于工厂,以便在查找时将优先使用该版本
bindAsContract(MyExceptionMapperFactory.class)
.to(ExceptionMappers.class)
.ranked(Integer.MAX_VALUE)
.in(Singleton.class);
更新
由于排名不起作用,以下内容似乎将删除原始工厂
@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;
}
}