Dropwizard + jersey:引起:java.lang.IllegalStateException:不在请求范围内。对于资源

时间:2017-03-03 06:23:57

标签: java jersey dropwizard

我在现有资源中添加了一个新方法来获取单个产品,之后我的dropwizard应用程序没有盯着并抛出以下错误: -

Caused by: java.lang.IllegalStateException: Not inside a request scope.
    at jersey.repackaged.com.google.common.base.Preconditions.checkState(Preconditions.java:173)
    at org.glassfish.jersey.process.internal.RequestScope.current(RequestScope.java:233)
    at org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:158)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2020)
    ... 63 more

并且

WARN  [2017-03-03 06:12:12,334] /: unavailable
! java.lang.IllegalStateException: Not inside a request scope.
! at jersey.repackaged.com.google.common.base.Preconditions.checkState(Preconditions.java:173)
! at org.glassfish.jersey.process.internal.RequestScope.current(RequestScope.java:233)
! at org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:158)
! at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2020)
! ... 63 common frames omitted
! Causing: org.glassfish.hk2.api.MultiException: A MultiException has 1 exceptions.  They are:
! 1. java.lang.IllegalStateException: Not inside a request scope.
! 
! at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2026)
! at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:766)
! at org.jvnet.hk2.internal.ServiceLocatorImpl.getUnqualifiedService(ServiceLocatorImpl.java:773)
! at org.jvnet.hk2.internal.IterableProviderImpl.get(IterableProviderImpl.java:111)
! at org.glassfish.jersey.server.internal.inject.AbstractContainerRequestValueFactory.getContainerRequest(AbstractContainerRequestValueFactory.java:71)
! at org.glassfish.jersey.server.internal.inject.PathParamValueFactoryProvider$PathParamValueFactory.provide(PathParamValueFactoryProvider.java:93)
! at org.glassfish.jersey.server.internal.inject.ParamInjectionResolver.resolve(ParamInjectionResolver.java:134)
! at org.jvnet.hk2.internal.Utilities.justInject(Utilities.java:961)
! at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(ServiceLocatorImpl.java:980)
! at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(ServiceLocatorImpl.java:970)
! at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:617)
! at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:184)
! at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:350)
! at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:347)
! at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
! at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
! at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255)
! at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:347)
! at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392)
! at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)
! at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369)
! at javax.servlet.GenericServlet.init(GenericServlet.java:244)
! at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:640)
! at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:419)
! at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:875)
! at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:349)
! at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:772)
! at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:262)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
! at com.codahale.metrics.jetty9.InstrumentedHandler.doStart(InstrumentedHandler.java:103)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
! at org.eclipse.jetty.server.handler.gzip.GzipHandler.doStart(GzipHandler.java:231)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
! at org.eclipse.jetty.server.handler.StatisticsHandler.doStart(StatisticsHandler.java:252)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
! at org.eclipse.jetty.server.Server.start(Server.java:411)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
! at org.eclipse.jetty.server.Server.doStart(Server.java:378)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:53)
! at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:44)
! at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:85)
! at io.dropwizard.cli.Cli.run(Cli.java:75)

```

下面是我的get方法的代码,使用它抛出异常: -

@GET
    @PathParam("{id}")
    @Timed
    public Response get(@PathParam("id") String id){
        Response.ResponseBuilder response = null;
        BaseResponse productResponse = new BaseResponse();
        try {
            Product product = productService.get(id);
            if(product != null)
            {
                productResponse.setOutput(product);
                response.status(200).entity(productResponse);

            }
        }catch (EntityNotPresentException e){
            AppError appError = new AppError();
            appError.setCode(50001);
            appError.setMessage(e.getMessage());
            productResponse.setError(appError);
            response.status(500).entity(productResponse);
        }

        return response.build();
    }

1 个答案:

答案 0 :(得分:0)

我刚刚使用存根代码复制并运行我的IntelliJ中的代码,并得到了同样的错误。在花了一些时间进行调试后,我发现get方法不是@Path注释,而是使用@PathParam注释。当我将其更改为正确的@Path注释时。它对我有用。

让我知道它是否能解决您的问题。