Jersey 2.x Anotación de inyección personalizada con atributos
Estoy en el proceso de migrar de DropWizard 0.7.1 a 0.8.1. Esto incluye la migración de Jersey 1.xa 2.x. En mi implementación que usa Jersey 1.18.1, tuve unMyProvider
(cambió todos los nombres de clase por simplicidad) que implementaInjectableProvider
. Esta clase crearíaMyInjectable
objetos, que contienen la anotación de inyección personalizada,MyToken
. MyToken
contiene varios atributos que son transmitidos y leídos porMyInjectable
. Por último, en elApplication
clase registro una nueva instancia deMyProvider
, como se ve a continuación.
He investigado un poco y parece que no puedo entender cómo recrearía (o reemplazaría, supongo) un escenario así en Jersey 2.x.
Aquí está la implementación actual, 1.18.1:
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER, ElementType.FIELD })
public @interface MyToken {
// Custom annotation containing various attributes
boolean someAttribute() default true;
// ...
}
public class MyProvider implements InjectableProvider<MyToken, Parameter> {
// io.dropwizard.auth.Authenticator
private final Authenticator<String, MyObject> authenticator;
public MyProvider(Authenticator<String, MyObject> authenticator) {
this.authenticator = authenticator;
}
@Override
public ComponentScope getScope() {
return ComponentScope.PerRequest;
}
@Override
public Injectable<?> getInjectable(ComponentContext ic, MyToken t, Parameter p) {
return new MyInjectable(authenticator, t.someAttribute());
}
}
class MyInjectable extends AbstractHttpContextInjectable<MyObject> {
private final Authenticator<String, Session> authenticator;
private final boolean someAttribute;
public MyInjectable(Authenticator<String, MyObject> authenticator, boolean someAttribute) {
this.authenticator = authenticator;
this.someAttribute = someAttribute;
// ... Removed a few paramters for simplicity's sake
}
@Override
public MyObject getValue(HttpContext c) {
final HttpRequestContext request = c.getRequest();
// ... Removed code not pertaining to the question
return myObject;
}
}
// Lastly, the register call in the io.dropwizard.Application class
environment.jersey().register(new MyProvider(new MyProviderValidator(someValidator)));