Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Dropwizard-Can';t使用Guice Injector.getIntance()注册jersey资源_Java_Rest_Jersey_Guice_Dropwizard - Fatal编程技术网

Java Dropwizard-Can';t使用Guice Injector.getIntance()注册jersey资源

Java Dropwizard-Can';t使用Guice Injector.getIntance()注册jersey资源,java,rest,jersey,guice,dropwizard,Java,Rest,Jersey,Guice,Dropwizard,我在以前的方法中成功地使用了这个设置,所以我现在可能做错了什么。我主要使用Guice作为其AOP部分 pom.xml的相关章节: <dependency> <groupId>io.dropwizard</groupId> <artifactId>dropwizard-core</artifactId> <version>0.7.0</version>

我在以前的方法中成功地使用了这个设置,所以我现在可能做错了什么。我主要使用Guice作为其AOP部分

pom.xml的相关章节:

    <dependency>
        <groupId>io.dropwizard</groupId>
        <artifactId>dropwizard-core</artifactId>
        <version>0.7.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.jboss.logging</groupId>
                <artifactId>jboss-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>4.0</version>
    </dependency>

    <dependency>
        <groupId>aopalliance</groupId>
        <artifactId>aopalliance</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.6</version>
        <scope>provided</scope>
    </dependency>
注册资源的文件:

public class BucService extends Application<BucConfiguration> {
    public static void main(String[] args) throws Exception {
        new BucService().run(args);
    }

    @Override
    public void initialize(Bootstrap<BucConfiguration> bootstrap) {
        //bootstrap.setName("hello-world");
    }

    @Override
    public void run(BucConfiguration configuration,
                    Environment environment) {
        final String template = configuration.getTemplate();
        final String defaultName = configuration.getDefaultName();
        environment.jersey().register(new HelloWorldResource(template, defaultName));

        Injector injector = Guice.createInjector(new BaseBindingModule ());
        environment.jersey().register(injector.getInstance(CabResource.class)); // this does not work
      //  environment.jersey().register(new CabResource()); // this works
    }
}
根据评论进一步澄清:

我确实使用injector.getInstance(CabResource.class)获得了一个实例

例如:

CabResource a = new CabResource();
CabResource b = injector.getInstance(CabResource.class);

a.getCheapestCabs(); 
b.getCheapestCabs();
这两个电话都有效。

您考虑过使用吗


com.hubspot.dropwizard

如果没有错误,添加错误日志可能会有所帮助。它只是不注册。我可以很好地运行这个罐子。我就是无法访问API。它说404找不到。我仍然添加了运行jar.injector.getInstance(CabResource.class)时得到的输出,这是否会返回任何东西?它可能不会返回任何东西?因为它还没有在Guice注册?我想我读得有点太快了。尝试将资源绑定到模块中,我们将对此进行研究。谢谢虽然在另一份工作的监督下,我成功地运行了相同的设置,而不需要这些库。因此,我真的希望在不使用其他外部库的情况下解决这个问题,并用当前代码找出问题所在。
@Path("/cabs")
@Produces(MediaType.APPLICATION_JSON)
public class CabResource {

    @Inject
    CabManager manager;


    public CabResource() {
    }

    @GET
    @Path("price")
    @Produces(MediaType.APPLICATION_JSON)
    public List<PriceEstimate> getCheapestCabs() {

        return manager.callB();
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @SendCrashReport
    public List<PriceEstimate> getNearestCabs(@Context UriInfo uriInfo) {

        return manager.callA();
      }

}
INFO  [2016-04-01 13:26:14,785] io.dropwizard.server.ServerFactory: Starting BucService
INFO  [2016-04-01 13:26:14,876] org.eclipse.jetty.setuid.SetUIDListener: Opened application@758a34ce{HTTP/1.1}{0.0.0.0:9000}
INFO  [2016-04-01 13:26:14,876] org.eclipse.jetty.setuid.SetUIDListener: Opened admin@7ec3394b{HTTP/1.1}{0.0.0.0:9001}
INFO  [2016-04-01 13:26:14,877] org.eclipse.jetty.server.Server: jetty-9.0.z-SNAPSHOT
INFO  [2016-04-01 13:26:14,965] com.sun.jersey.server.impl.application.WebApplicationImpl: Initiating Jersey application, version 'Jersey: 1.18.1 02/19/2014 03:28 AM'
INFO  [2016-04-01 13:26:15,060] io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:

    GET     /hello-world (com.buc.resources.HelloWorldResource)

INFO  [2016-04-01 13:26:15,262] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@6c451c9c{/,null,AVAILABLE}
INFO  [2016-04-01 13:26:15,263] io.dropwizard.setup.AdminEnvironment: tasks = 

    POST    /tasks/gc (io.dropwizard.servlets.tasks.GarbageCollectionTask)

WARN  [2016-04-01 13:26:15,264] io.dropwizard.setup.AdminEnvironment: 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!    THIS APPLICATION HAS NO HEALTHCHECKS. THIS MEANS YOU WILL NEVER KNOW      !
!     IF IT DIES IN PRODUCTION, WHICH MEANS YOU WILL NEVER KNOW IF YOU'RE      !
!    LETTING YOUR USERS DOWN. YOU SHOULD ADD A HEALTHCHECK FOR EACH OF YOUR    !
!         APPLICATION'S DEPENDENCIES WHICH FULLY (BUT LIGHTLY) TESTS IT.       !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
INFO  [2016-04-01 13:26:15,269] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@3dddbe65{/,null,AVAILABLE}
INFO  [2016-04-01 13:26:15,279] org.eclipse.jetty.server.ServerConnector: Started application@758a34ce{HTTP/1.1}{0.0.0.0:9000}
INFO  [2016-04-01 13:26:15,280] org.eclipse.jetty.server.ServerConnector: Started admin@7ec3394b{HTTP/1.1}{0.0.0.0:9001}
CabResource a = new CabResource();
CabResource b = injector.getInstance(CabResource.class);

a.getCheapestCabs(); 
b.getCheapestCabs();
<dependency>
  <groupId>com.hubspot.dropwizard</groupId>
  <artifactId>dropwizard-guice</artifactId>
  <version>${dropwizard.guice.version}</version>
</dependency>