gpt4 book ai didi

java - Jersey :如何使用自定义 `ExceptionMapperFactory`

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:04:41 25 4
gpt4 key购买 nike

我需要使用自定义 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!

最佳答案

我从来没有实现过这个,所以我不知道所有的细微差别,但简要地看一下 sourcetests , 看起来你只需要 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;
}
}

关于java - Jersey :如何使用自定义 `ExceptionMapperFactory`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40804651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com