6

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

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 在第二次调用中不起作用的问题将不是问题。

4

1 回答 1

3

我遇到了完全相同的问题 - 两次调用带注释的资源方法。经过深入调查,我找到了方法,如何在 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());
    }
}

希望它会有所帮助:)

于 2013-11-29T14:53:14.917 回答