我一直在关注这个问题并找到解决方案,并希望为随后的任何其他勇敢的JAX-RS冒险者提供反馈。
错误:
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)
....
它是一个标记为@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();
}
....
答案 0 :(得分:3)
答案是@Context
上的URIInfo
。它必须像:
public void setUriInfo(@Context UriInfo uriInfo) {
System.out.println("UserResource - set uriInfo:"+uriInfo);
this.uriInfo = uriInfo;
}
或
@Context
protected UriInfo uriInfo;
似乎很明显不是吧!这个例外提到了URIInfo,那么我的问题是什么?我不知道,我只能认为我看到的东西与此错误完全一样,由其他东西引起,我改变了一些不正确的东西(比如你在顶部看到的大块代码)。异常似乎是相同的,并没有点击我实际上导致了不同的错误。
无论如何,我走开了,在我的两腿之间徘徊,感到有点高兴我找出问题的根源和解决方案。 '网上什么都没有,所以我想我会和你分享这一切。