为了在Jersey 2中共存两个自定义注入注释,我应该如何进行ValueFactoryProvider绑定?下面我已经包含了一个当前方法的示例,您可以看到Hello注释注入“隐藏”了SmallTalk注释注入。
Hello注释:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface Hello {
}
SmallTalk注释:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface SmallTalk {
}
Hello注释解析器:
@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注释解析器:
@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);
}
}
}
资源配置:
public class MyApplication extends ResourceConfig {
public MyApplication() {
register(new HelloResolver.Binder());
register(new SmallTalkResolver.Binder());
registerClasses(HelloResource.class);
}
}
使用注入注释的资源:
@Path("/")
public class HelloResource {
@GET
@Path("hello")
@Produces("application/json")
public String hello(@Hello final String hello, @SmallTalk final String smallTalk) {
return hello + " " + smallTalk;
}
}
请求资源时的结果 - 应该是"Hello! Nice weather."
:
答案 0 :(得分:4)
找到解决方案!我添加了
if (parameter.getAnnotation(Hello.class) == null) return null;
和
if (parameter.getAnnotation(SmallTalk.class) == null) return null;
到两个价值工厂提供商的createValueFactory
方法。