Why is my Jersey JAX-RS server throwing a IllegalStateException about not being in RequestScope?

柔情痞子 提交于 2019-12-11 03:35:19

问题


I've been looking at this one for too long and found the solution and wanted to provide feedback for any other intrepid JAX-RS adventurers that follow.

Error:

java.lang.IllegalStateException: Not inside a request scope.
    at com.google.common.base.Preconditions.checkState(Preconditions.java:149)
    at org.glassfish.jersey.process.internal.RequestScope.current(RequestScope.java:226)
    at org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:154)
    at org.jvnet.hk2.internal.MethodInterceptorImpl.intercept(MethodInterceptorImpl.java:80)
    at org.glassfish.jersey.internal.inject.UriInfoInjectee$$EnhancerByCGLIB$$4cfd1aab.toString(<generated>)
    at java.lang.String.valueOf(String.java:2854)
    at java.lang.StringBuilder.append(StringBuilder.java:128)
    at au.csiro.esa.authrest.rest.resource.PingResource.setUriInfo(PingResource.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.glassfish.hk2.utilities.reflection.ReflectionHelper.invoke(ReflectionHelper.java:1011)
    at org.jvnet.hk2.internal.Utilities.justInject(Utilities.java:816)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(ServiceLocatorImpl.java:801)
    at org.glassfish.jersey.gf.cdi.CdiComponentProvider$1.inject(CdiComponentProvider.java:316)
    at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:158)
    at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:103)
    at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:93)
    at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:79)
    at au.csiro.esa.authrest.rest.resource.PingResource$Proxy$_$$_WeldClientProxy.ping(Unknown Source)
....

Its a simple Resource marked as @RequestScoped

@Path("ping")
@RequestScoped
public class PingResource {

    protected UriInfo uriInfo;

    @Context
    public void setUriInfo(UriInfo uriInfo) {
        System.out.println("UserResource - set uriInfo:"+uriInfo);
        this.uriInfo = uriInfo;
    }

    @Inject
    @PropertiesFile(name = "app")
    Properties properties;

    @PermitAll
    @GET
    @Produces({ MediaType.TEXT_PLAIN })
    public Response ping() {
        System.out.println("Hit ping");
        return Response.ok().entity("Running version " + properties.getProperty("application.version")).build();
        // return Response.ok().entity("Running version 10").build();
    }
    ....

回答1:


The answer is the @Context on the URIInfo. It has to be like:

public void setUriInfo(@Context UriInfo uriInfo) {
        System.out.println("UserResource - set uriInfo:"+uriInfo);
        this.uriInfo = uriInfo;
    }

or

@Context
protected UriInfo uriInfo;

Seems obvious doesn't it! The exception mentions URIInfo so what was my problem? I dunno, I can only think that I saw something exactly like this error, caused by something else and I changed things around to be incorrect (like in the big block of code you see at the top). The exception seemed to be the same and it didn't click that I'd actually caused a different error.

Anyway, I'm walking away, tail between my legs and feeling somewhat happy that I worked out the cause of the problem and the solution. There was nothing on the 'net so I thought I'd share this with you 'all.



来源:https://stackoverflow.com/questions/20119108/why-is-my-jersey-jax-rs-server-throwing-a-illegalstateexception-about-not-being

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