eclipse-ee4j / jersey

Eclipse Jersey Project - Read our Wiki:
https://github.com/eclipse-ee4j/jersey/wiki
Other
692 stars 355 forks source link

Starting error `Not inside a request scope` with Jersey 2.34 & 3.0.2 #4787

Closed nhenneaux closed 3 years ago

nhenneaux commented 3 years ago

With new version 2.34 & 3.0.2 I'm getting the error Not inside a request scope when starting the application.

Version 3.0.1 & 2.33 are working fine.


java.lang.IllegalStateException: Not inside a request scope.
    at org.glassfish.jersey.internal.guava.Preconditions.checkState(Preconditions.java:169)
    at org.glassfish.jersey.process.internal.RequestScope.current(RequestScope.java:153)
    at org.glassfish.jersey.inject.hk2.RequestContext.findOrCreate(RequestContext.java:55)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2103)
    at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:93)
    at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:67)
    at org.glassfish.jersey.inject.hk2.ContextInjectionResolverImpl.resolve(ContextInjectionResolverImpl.java:103)
    at org.jvnet.hk2.internal.Utilities.justInject(Utilities.java:989)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(ServiceLocatorImpl.java:1014)
    at org.glassfish.jersey.inject.hk2.AbstractHk2InjectionManager.inject(AbstractHk2InjectionManager.java:207)
    at org.glassfish.jersey.inject.hk2.ImmediateHk2InjectionManager.inject(ImmediateHk2InjectionManager.java:30)
    at org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider$InjectionManagerInjectedCdiTarget.inject(CdiComponentProvApr 29 18:37:03 Remove shutdown hook ShutdownHook{timeout=300000, priority=3000, runnable=tech.ingenico.esf.cdi.CdiContainer$$Lambda$527/0x0000000800eb96f0@113e498b} from shutdown hook set.
ider.java:769)
    at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:161)
    at org.jboss.weld.contexts.unbound.DependentContextImpl.get(DependentContextImpl.java:64)
    at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)
    at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:67)
    at org.jboss.weld.event.ObserverMethodImpl.getReceiver(ObserverMethodImpl.java:360)
    at org.jboss.weld.event.ObserverMethodImpl.getReceiverIfExists(ObserverMethodImpl.java:347)
    at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:305)
    at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:286)
    at jakarta.enterprise.inject.spi.ObserverMethod.notify(ObserverMethod.java:125)
    at org.jboss.weld.util.Observers.notify(Observers.java:166)
    at org.jboss.weld.event.ObserverNotifier.notifySyncObservers(ObserverNotifier.java:285)
    at org.jboss.weld.event.ObserverNotifier.notify(ObserverNotifier.java:273)
    at org.jboss.weld.event.EventImpl.fire(EventImpl.java:96)
    at tech.ingenico.esf.http.api.server.management.ServerState.started(ServerState.java:113)
    at tech.ingenico.esf.http.jetty.server.JettyServer.lambda$builder$3(JettyServer.java:107)
    at tech.ingenico.esf.http.jetty.server.JettyServer$Builder.start(JettyServer.java:383)
    at tech.ingenico.esf.akms.Bootstrap.main(Bootstrap.java:36)
    at tech.ingenico.esf.akms.BootstrapTest.lambda$getBootstrapRunnable$0(BootstrapTest.java:12)
    at java.base/java.lang.Thread.run(Thread.java:832)
nhenneaux commented 3 years ago

It was due to a JAX-RS/CDI bean with HttpServletRequest injected using @Context. I replaced it with a provider called when used to provide the request.


@Provider
public class HttpServletRequestProducer {

    @Context
    private ResourceContext resourceContext;

    @Produces
    HttpServletRequest request() {
        return resourceContext.getResource(HttpServletRequest.class);
    }
jansupol commented 3 years ago

@nhenneaux Are you saying you were able to overcome a bug, or your app code has changed since 2.33? If there is a bug, I would very much love to see a reproducer.

nhenneaux commented 3 years ago

I had to change the application to work with 2.34/3.0.2. However the class was a bit strange mixing CDI and JAXRS annotations.

jansupol commented 3 years ago

Mixing 2 injection frameworks is always strange, but even more strange is that it stopped working in 2.34 for you. Can you share more details about the app?

nhenneaux commented 3 years ago

It was due to a JAX-RS/CDI bean with HttpServletRequest injected using @Context. I replaced it with a provider called when used to provide the request.


@Alternative
@Priority(Interceptor.Priority.LIBRARY_BEFORE)
@Path("")
public class Service {

    @Context
    HttpServletRequest request;