泽西岛:如何使用自定义的“ExceptionMapperFactory”
Jersey: How to use a custom `ExceptionMapperFactory`
我需要使用自定义 ExceptionMapperFactory
来实现自定义 find()
逻辑。
public class MyExceptionMapperFactory extends ExceptionMapperFactory {
// [...]
@Override
public <T extends Throwable> ExceptionMapper<T> find(Class<T> type) {
// [...]
}
}
我怎样才能use/register呢?
在我的 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 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);
}
})
不确定如何禁用原始的,但如果它仍在使用,您可以尝试 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;
}
}
我需要使用自定义 ExceptionMapperFactory
来实现自定义 find()
逻辑。
public class MyExceptionMapperFactory extends ExceptionMapperFactory {
// [...]
@Override
public <T extends Throwable> ExceptionMapper<T> find(Class<T> type) {
// [...]
}
}
我怎样才能use/register呢?
在我的 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 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);
}
})
不确定如何禁用原始的,但如果它仍在使用,您可以尝试 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;
}
}