2015-03-26 8 views
5

Ho un problema simile a quello sollevato qui: How do I get my Jersey 2 Endpoints to eagerly initialize on startup?uso annotazioni @Immediate in Jersey2

ma un po 'più in basso la linea. Posso caricare immediatamente la mia risorsa, ma quando provo a usarla chiamando l'URL REST, ottengo la seguente traccia dello stack.

java.lang.IllegalStateException: Could not find an active context for org.glassfish.hk2.api.Immediate 
2. java.lang.IllegalStateException: While attempting to create a service for  
SystemDescriptor(
implementation=com.service.MyResource 
contracts={com.service.MyResource} 
scope=org.glassfish.hk2.api.Immediate 
qualifiers={} 
descriptorType=CLASS 
descriptorVisibility=NORMAL 
metadata= 
rank=0 
loader=null 
proxiable=null 
proxyForSameScope=null 
analysisName=null 
id=150 
locatorId=0 
identityHashCode=1249600275 
reified=true) in scope org.glassfish.hk2.api.Immediate an error occured while locating the context 

La mia classe TResource è annotata in tal modo:

@Immediate 
@Path("/test/v1") 
public class TResource { 

mio server basato su Grizzly è configurato in questo modo:

ResourceConfig rc = new ResourceConfig() 
      .packages(true, 
        "com.mystuff" 
      )   
      .property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true"); 

    HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(base_uri), rc); 

    ApplicationHandler handler = new ApplicationHandler(rc); 
    ServiceLocatorUtilities.enableImmediateScope(handler.getServiceLocator()); 

Qualsiasi orientamento sarebbe più apprezzati! applausi, Phil

risposta

7

un modo per ottenere una maniglia sul ServiceLocator è quello di implementare un Feature.

import javax.inject.Inject; 
import javax.ws.rs.core.Feature; 
import javax.ws.rs.core.FeatureContext; 
import org.glassfish.hk2.api.ServiceLocator; 
import org.glassfish.hk2.utilities.ServiceLocatorUtilities; 

public class ImmediateFeature implements Feature { 

    @Inject 
    public ImmediateFeature(ServiceLocator locator) { 
     ServiceLocatorUtilities.enableImmediateScope(locator);    
    } 

    @Override 
    public boolean configure(FeatureContext context) { 
     return true; 
    } 
} 

Poi basta registrare il Feature

ResourceConfig rc = new ResourceConfig().packages("jersey.hk2.test"); 
rc.register(ImmediateFeature.class); 

Ho provato questo e funziona benissimo

+0

Grazie! Questo risolve davvero il problema. Molto apprezzato!. Phil – Phil

Problemi correlati