Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two custom injection annotations in Jersey 2

How should I do the ValueFactoryProvider binding in order to have two custom injection annotations coexist in Jersey 2? Below I have included an example of my current approach and as you can see the Hello annotation injection "hides" the SmallTalk annotation injection.

Hello annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface Hello {
}

SmallTalk annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface SmallTalk {
}

Hello annotation resolver:

@Singleton
public class HelloResolver {
    public static class HelloInjectionResolver extends ParamInjectionResolver<Hello> {
        public HelloInjectionResolver() {
            super(HelloValueFactoryProvider.class);
        }
    }

    @Singleton
    public static class HelloValueFactoryProvider extends AbstractValueFactoryProvider {
        @Inject
        public HelloValueFactoryProvider(final MultivaluedParameterExtractorProvider extractorProvider,
                                         final ServiceLocator injector) {
            super(extractorProvider, injector, UNKNOWN);
        }

        @Override
        protected Factory<?> createValueFactory(final Parameter parameter) {
            final Class<?> classType = parameter.getRawType();

            if (classType == null || (!classType.equals(String.class))) return null;

            return new AbstractContainerRequestValueFactory<String>() {
                @Override
                public String provide() {
                    return "Hello!";
                }
            };
        }
    }

    public static class Binder extends AbstractBinder {
        @Override
        protected void configure() {
            bind(HelloValueFactoryProvider.class).to(ValueFactoryProvider.class).in(Singleton.class);
            bind(HelloInjectionResolver.class).to(
                    new TypeLiteral<InjectionResolver<Hello>>() {
                    }
            ).in(Singleton.class);
        }
    }
}

SmallTalk annotation resolver:

@Singleton
public class SmallTalkResolver {
    public static class SmallTalkInjectionResolver extends ParamInjectionResolver<SmallTalk> {
        public SmallTalkInjectionResolver() {
            super(SmallTalkValueFactoryProvider.class);
        }
    }

    @Singleton
    public static class SmallTalkValueFactoryProvider extends AbstractValueFactoryProvider {
        @Inject
        public SmallTalkValueFactoryProvider(final MultivaluedParameterExtractorProvider extractorProvider,
                                             final ServiceLocator injector) {
            super(extractorProvider, injector, UNKNOWN);
        }

        @Override
        protected Factory<?> createValueFactory(final Parameter parameter) {
            final Class<?> classType = parameter.getRawType();

            if (classType == null || (!classType.equals(String.class))) return null;

            return new AbstractContainerRequestValueFactory<String>() {
                @Override
                public String provide() {
                    return "Nice weather.";
                }
            };
        }
    }

    public static class Binder extends AbstractBinder {
        @Override
        protected void configure() {
            bind(SmallTalkValueFactoryProvider.class).to(ValueFactoryProvider.class).in(Singleton.class);
            bind(SmallTalkInjectionResolver.class).to(
                    new TypeLiteral<InjectionResolver<SmallTalk>>() {
                    }
            ).in(Singleton.class);
        }
    }
}

Resource configuration:

public class MyApplication extends ResourceConfig {
    public MyApplication() {
        register(new HelloResolver.Binder());
        register(new SmallTalkResolver.Binder());
        registerClasses(HelloResource.class);
    }
}

Resource using both injection annotations:

@Path("/")
public class HelloResource {
    @GET
    @Path("hello")
    @Produces("application/json")
    public String hello(@Hello final String hello, @SmallTalk final String smallTalk) {
        return hello + " " + smallTalk;
    }
}

Result when requesting the resource - should have been "Hello! Nice weather.":

like image 736
Stine Avatar asked Dec 15 '13 22:12

Stine


1 Answers

Found a solution! I added

if (parameter.getAnnotation(Hello.class) == null) return null;

and

if (parameter.getAnnotation(SmallTalk.class) == null) return null;

to the createValueFactory method of the two value factory providers.

like image 188
Stine Avatar answered Oct 14 '22 10:10

Stine