InjectionResolver with Jersey 2.x - The resource is called twice

I am trying to figure out how to use custom annotation and HK2 to input something into the Resource method. Because I'm in the Spring webapp environment, I just bumped into an existing helloworld-spring -webapp Jersey 2 example. My problem is that the Resource method is called twice. The first time the injection is successful, the second time it is not.

InjectionResolver.resolve () Method

@Override
public Object resolve(Injectee injectee, ServiceHandle<?> root) {
    return "THIS HAS BEEN INJECTED APPROPRIATELY";
}

Binder.configure () Method

@Override
protected void configure() {
    bind(SampleInjectionResolver.class).to(new TypeLiteral<InjectionResolver<SampleParam>>() {}).in(Singleton.class);
}

ResourceConfig binder registration

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

Deriving a Server from a SINGLE Call

EXECUTING!
*******************************INJECTED: THIS HAS BEEN INJECTED APPROPRIATELY
EXECUTING!
*******************************INJECTED:

Am I missing a configuration somewhere? I can’t understand why his name is twice. I assume that if I fix this, a problem with InjectionResolver not working on the second call would be a problem without problems.

+4
1

- . , 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());
    }
}

, :)

+3

All Articles