gpt4 book ai didi

java - Jersey 2.x 的 InjectionResolver——资源被调用两次

转载 作者:搜寻专家 更新时间:2023-11-01 03:06:56 26 4
gpt4 key购买 nike

我正在尝试弄清楚如何使用自定义注释和 HK2 将某些内容注入(inject) Resource 方法。因为我在 Spring webapp 环境中,所以我只是在现有的 helloworld-spring-webapp Jersey 2 示例上堆砌。我的问题是,Resource 方法被调用了两次。第一次,注入(inject)成功,第二次,没有。

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);
}

Binder的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 - Jersey 2.x 的 InjectionResolver——资源被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19118604/

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