@Immediate annotation use in Jersey2

十年热恋 提交于 2019-12-19 08:12:40

问题


I have a similar issue to the one raised here: How do I get my Jersey 2 Endpoints to eagerly initialize on startup?

But slightly further down the line. I can get my resource immediately loaded, but when I try to use it by calling the REST url, I get the following stack trace.

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

My TResource class is annotated thus:

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

My Grizzly based server is setup like so:

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());

Any guidance would be most appreciated ! cheers, Phil


回答1:


One way to get a handle on the ServiceLocator is to implement a 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;
    }  
}

Then just register the Feature

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

I've tested this and it works fine



来源:https://stackoverflow.com/questions/29289245/immediate-annotation-use-in-jersey2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!