java – 使用Jersey 2.x的InjectionResolver – 资源被调用两次
作者:互联网
我试图弄清楚如何使用自定义注释和HK2将一些东西注入到Resource方法中.因为我在Spring webapp环境中,所以我只是在现有的helloworld-spring-webapp Jersey 2示例上.我的问题是,Resource方法是否被调用两次.第一次,注射成功,第二次,它没有.
InjectionResolver.resolve()方法
@Override
public Object resolve(Injectee injectee, ServiceHandle<?> root) {
return "THIS HAS BEEN INJECTED APPROPRIATELY";
}
Binder.configure()方法
@Override
protected void configure() {
bind(SampleInjectionResolver.class).to(new TypeLiteral<InjectionResolver<SampleParam>>() {}).in(Singleton.class);
}
ResourceConfig注册活页夹
public MyApplication () {
register(new SampleInjectionResolver.Binder());
...
JerseyResource.getHello()
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getHello(@SampleParam String inject) {
System.err.println("EXECUTING!");
System.err.println("*******************************INJECTED: " + inject);
return inject;
}
来自SINGLE呼叫的服务器输出
EXECUTING!
*******************************INJECTED: THIS HAS BEEN INJECTED APPROPRIATELY
EXECUTING!
*******************************INJECTED:
我错过了某处的配置吗?我无法弄清楚为什么它会被调用两次.我假设如果我解决了这个问题,那么在第二次调用中没有工作的InjectionResolver的问题将是一个非问题.
解决方法:
我遇到了完全相同的问题 – 两次调用带注释的资源方法.
经过深入调查,我找到了方法,如何在Jersey 2.x中使用自定义注释.
自定义注释类(TestMessage.java):
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER })
public @interface TestMessage {}
自定义注释解析器(TestMessageResolver.java):
public class TestMessageResolver {
// InjectionResolver implementation
public static class TestMessageInjectionResolver extends ParamInjectionResolver<TestMessage> {
public TestMessageInjectionResolver() {
super(TestMessageValueFactoryProvider.class);
}
}
// ValueFactoryProvider implementation
@Singleton
public static class TestMessageValueFactoryProvider extends AbstractValueFactoryProvider {
@Inject
public TestMessageValueFactoryProvider(MultivaluedParameterExtractorProvider mpep, ServiceLocator injector) {
super(mpep, injector, Parameter.Source.UNKNOWN);
}
@Override
protected Factory<?> createValueFactory(Parameter parameter) {
Class<?> classType = parameter.getRawType();
if (classType == null || (!classType.equals(String.class))) {
return null;
}
return new AbstractContainerRequestValueFactory<String>() {
@Override
public String provide() {
return "testString";
}
};
}
}
// Binder implementation
public static class Binder extends AbstractBinder {
@Override
protected void configure() {
bind(TestMessageValueFactoryProvider.class).
to(ValueFactoryProvider.class).
in(Singleton.class);
bind(TestMessageInjectionResolver.class).
to(new TypeLiteral<InjectionResolver<TestMessage>>(){}).
in(Singleton.class);
}
}
}
自定义注释用法(JerseyResource.java):
@Path("jersey")
public class JerseyResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getMethod(@TestMessage String message) {
return "getMethod(), message=" + message;
}
}
解析器注册(SpringJerseyApplication.java):
public class SpringJerseyApplication extends ResourceConfig {
public SpringJerseyApplication() {
register(JerseyResource.class);
register(new TestMessageResolver.Binder());
}
}
希望它会有所帮助:)
标签:java,spring,jax-rs,jersey-2-0,hk2 来源: https://codeday.me/bug/20190629/1325176.html