Sto cercando di creare un bind personalizzato http per il mio servizio restful. Si prega di vedere l'esempio qui sotto.jersey 2: Come creare un binding param personalizzato HTTP
@POST
@Path("/user/{userId}/orders")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public MyResult foo(@PathParam("userId") String someString, @UserAuthHeaderParam String authString){
}
Si può vedere che c'è un'annotazione UserAuthHeaderParam nella firma della funzione. Quello che voglio fare è avere un bind personalizzato http diverso da quello standard javax.ws.rs. * Param.
devo cercare di attuare org.glassfish.hk2.api.InjectionResolver che fondamentalmente estrarre il valore da intestazione http:
public class ProtoInjectionResolver implements InjectionResolver<UserAuthHeaderParam>{
...
@Override
public Object resolve(Injectee injectee, ServiceHandle<?> root)
{
return "Hello World";
}
...
}
Quando chiamo il servizio riposante, il server ottiene sotto eccezioni. Indica che il framework non riesce a risolvere il parametro nella firma della funzione:
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=String,parent=MyResource,qualifiers={}),position=0,optional=false,self=false,unqualified=null,2136594195),
java.lang.IllegalArgumentException: While attempting to resolve the dependencies of rs.server.MyResource errors were found
Please help. Qualsiasi consiglio è apprezzato. Faccio molte ricerche su google ma non riesco a farlo funzionare. Jersey 2.*. How to replace InjectableProvider and AbstractHttpContextInjectable of Jersey 1.* potrebbe essere la domanda simile.
- AGGIORNAMENTI: Io uso AbstractBinder di legare il mio risolutore di UserAuthHeaderParam:
public class MyApplication extends ResourceConfig
{
public MyApplication()
{
register(new AbstractBinder()
{
@Override
protected void configure()
{
// bindFactory(UrlStringFactory.class).to(String.class);
bind(UrlStringInjectResolver.class).to(new TypeLiteral<InjectionResolver<UrlInject>>()
{
}).in(Singleton.class);
}
});
packages("rs");
}
}
Grazie!
Dov'è il @Service? –
@MingtaoZhang I registro un AbstractBinder. modifica la mia domanda per aggiungere questi dettagli – yzandrew
quale versione di jersey jar stai usando? lo stai eseguendo su qualsiasi server di applicazioni o grizzly? –