Sti*_*ine 5 java annotations inject jersey hk2
我应该如何进行ValueFactoryProvider绑定,以便在Jersey 2中共存两个自定义注入注释?下面我已经包含了一个当前方法的示例,您可以看到Hello注释注入"隐藏"SmallTalk注释注入.
你好注释:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface Hello {
}
Run Code Online (Sandbox Code Playgroud)
SmallTalk注释:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface SmallTalk {
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
资源配置:
public class MyApplication extends ResourceConfig {
public MyApplication() {
register(new HelloResolver.Binder());
register(new SmallTalkResolver.Binder());
registerClasses(HelloResource.class);
}
}
Run Code Online (Sandbox Code Playgroud)
使用注入注释的资源:
@Path("/")
public class HelloResource {
@GET
@Path("hello")
@Produces("application/json")
public String hello(@Hello final String hello, @SmallTalk final String smallTalk) {
return hello + " " + smallTalk;
}
}
Run Code Online (Sandbox Code Playgroud)
请求资源时的结果 - 应该是"Hello! Nice weather.":

找到解决办法了!我添加了
if (parameter.getAnnotation(Hello.class) == null) return null;
Run Code Online (Sandbox Code Playgroud)
和
if (parameter.getAnnotation(SmallTalk.class) == null) return null;
Run Code Online (Sandbox Code Playgroud)
到createValueFactory两个值工厂提供者的方法。
| 归档时间: |
|
| 查看次数: |
2666 次 |
| 最近记录: |