注册资源中的Dropwizard java.lang.NoSuchMethodException

时间:2016-06-13 15:23:09

标签: rest jersey jetty jax-rs dropwizard

我使用最新的稳定版Dropwizard框架和Java 7.我尝试使用资源类,无论我尝试过什么,我都得到以下的堆栈跟踪:

WARN  [2016-06-13 14:51:26,664] org.eclipse.jetty.server.HttpChannel: /users/list
! java.lang.NoSuchMethodException: Could not find a suitable constructor in my.test.package.endpoint.TestResource class.
! at org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getConstructor(JerseyClassAnalyzer.java:192) ~[jersey-common-2.21.jar:na]
! at org.jvnet.hk2.internal.Utilities.getConstructor(Utilities.java:179) ~[hk2-locator-2.4.0-b31.jar:na]
! at org.jvnet.hk2.internal.Utilities.justCreate(Utilities.java:993) ~[hk2-locator-2.4.0-b31.jar:na]
! ... 65 common frames omitted
! Causing: org.glassfish.hk2.api.MultiException: A MultiException has 1 exceptions.  They are:
! 1. java.lang.NoSuchMethodException: Could not find a suitable constructor in my.test.package.endpoint.TestResource class.
! 
! at org.jvnet.hk2.internal.Collector.throwIfErrors(Collector.java:89) ~[hk2-locator-2.4.0-b31.jar:na]
! at org.jvnet.hk2.internal.Utilities.justCreate(Utilities.java:995) ~[hk2-locator-2.4.0-b31.jar:na]
! at org.jvnet.hk2.internal.ServiceLocatorImpl.create(ServiceLocatorImpl.java:963) ~[hk2-locator-2.4.0-b31.jar:na]
! at org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:1055) ~[hk2-locator-2.4.0-b31.jar:na]
! at org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:1047) ~[hk2-locator-2.4.0-b31.jar:na]
! at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:170) ~[jersey-common-2.21.jar:na]
! at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:284) ~[jersey-server-2.21.jar:na]
! at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:74) ~[jersey-server-2.21.jar:na]
! at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:109) ~[jersey-server-2.21.jar:na]
! at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) ~[jersey-server-2.21.jar:na]
! at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) ~[jersey-server-2.21.jar:na]
! at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) ~[jersey-server-2.21.jar:na]
! at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) ~[jersey-server-2.21.jar:na]
! at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:92) ~[jersey-server-2.21.jar:na]
! at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:61) ~[jersey-server-2.21.jar:na]
! at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197) ~[jersey-common-2.21.jar:na]
! at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:301) ~[jersey-server-2.21.jar:na]
! at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) ~[jersey-common-2.21.jar:na]
! at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) ~[jersey-common-2.21.jar:na]
! at org.glassfish.jersey.internal.Errors.process(Errors.java:315) ~[jersey-common-2.21.jar:na]
! at org.glassfish.jersey.internal.Errors.process(Errors.java:297) ~[jersey-common-2.21.jar:na]
! at org.glassfish.jersey.internal.Errors.process(Errors.java:267) ~[jersey-common-2.21.jar:na]
! at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317) ~[jersey-common-2.21.jar:na]
! at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:292) ~[jersey-server-2.21.jar:na]
! at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1139) ~[jersey-server-2.21.jar:na]
! at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471) ~[jersey-container-servlet-core-2.22.1.jar:na]
! ... 41 common frames omitted
! Causing: javax.servlet.ServletException: A MultiException has 1 exceptions.  They are:
! 1. java.lang.NoSuchMethodException: Could not find a suitable constructor in my.test.package.endpoint.TestResource class.
! 
! at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:487) ~[jersey-container-servlet-core-2.22.1.jar:na]
! at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425) ~[jersey-container-servlet-core-2.22.1.jar:na]
! at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383) ~[jersey-container-servlet-core-2.22.1.jar:na]
! at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336) ~[jersey-container-servlet-core-2.22.1.jar:na]
! at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223) ~[jersey-container-servlet-core-2.22.1.jar:na]
! at io.dropwizard.jetty.NonblockingServletHolder.handle(NonblockingServletHolder.java:49) ~[dropwizard-jetty-0.9.2.jar:0.9.2]
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1669) ~[jetty-servlet-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.servlets.UserAgentFilter.doFilter(UserAgentFilter.java:83) ~[jetty-servlets-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.servlets.GzipFilter.doFilter(GzipFilter.java:364) ~[jetty-servlets-9.2.13.v20150730.jar:9.2.13.v20150730]
! at io.dropwizard.jetty.BiDiGzipFilter.doFilter(BiDiGzipFilter.java:132) ~[dropwizard-jetty-0.9.2.jar:0.9.2]
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) ~[jetty-servlet-9.2.13.v20150730.jar:9.2.13.v20150730]
! at io.dropwizard.servlets.ThreadNameFilter.doFilter(ThreadNameFilter.java:29) ~[dropwizard-servlets-0.9.2.jar:0.9.2]
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) ~[jetty-servlet-9.2.13.v20150730.jar:9.2.13.v20150730]
! at io.dropwizard.jersey.filter.AllowedMethodsFilter.handle(AllowedMethodsFilter.java:43) ~[dropwizard-jersey-0.9.2.jar:0.9.2]
! at io.dropwizard.jersey.filter.AllowedMethodsFilter.doFilter(AllowedMethodsFilter.java:38) ~[dropwizard-jersey-0.9.2.jar:0.9.2]
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) ~[jetty-servlet-9.2.13.v20150730.jar:9.2.13.v20150730]
! at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:119) ~[guice-servlet-4.0.jar:na]
! at com.google.inject.servlet.GuiceFilter$1.call(GuiceFilter.java:133) ~[guice-servlet-4.0.jar:na]
! at com.google.inject.servlet.GuiceFilter$1.call(GuiceFilter.java:130) ~[guice-servlet-4.0.jar:na]
! at com.google.inject.servlet.GuiceFilter$Context.call(GuiceFilter.java:203) ~[guice-servlet-4.0.jar:na]
! at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:130) ~[guice-servlet-4.0.jar:na]
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) ~[jetty-servlet-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585) ~[jetty-servlet-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) ~[jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) ~[jetty-servlet-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) ~[jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) ~[jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) ~[jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]
! at com.codahale.metrics.jetty9.InstrumentedHandler.handle(InstrumentedHandler.java:240) ~[metrics-jetty9-3.1.2.jar:3.1.2]
! at io.dropwizard.jetty.RoutingHandler.handle(RoutingHandler.java:51) ~[dropwizard-jetty-0.9.2.jar:0.9.2]
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) ~[jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:95) ~[jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) ~[jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.server.handler.StatisticsHandler.handle(StatisticsHandler.java:159) ~[jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) ~[jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.server.Server.handle(Server.java:499) ~[jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310) ~[jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) [jetty-io-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) [jetty-util-9.2.13.v20150730.jar:9.2.13.v20150730]
! at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) [jetty-util-9.2.13.v20150730.jar:9.2.13.v20150730]
! at java.lang.Thread.run(Unknown Source) [na:1.7.0_80]
WARN  [2016-06-13 14:51:26,665] org.eclipse.jetty.server.HttpChannel: Could not send response error 500: javax.servlet.ServletException: A MultiException has 1 exceptions.  They are:
1. java.lang.NoSuchMethodException: Could not find a suitable constructor in my.test.package.endpoint.TestResource class.

虽然我已经阅读了许多类似的问题并尝试了所有可能的解决方案。问题依然存在。

内部应用程序类:

    @Override
        public void run(ServiceConfiguration configuration, Environment environment)
                throws Exception {
           final DBIFactory factory = new DBIFactory();
           final DBI jdbi = factory
          .build(environment, configuration.getDataSourceFactory(),"as400");
            final TestDAOdao = jdbi.onDemand(TestDAO.class);

            environment.jersey().register(new TestResource(dao));
    }

TestResource.class

@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class TestResource {

    private  final TestDAO testDAO;

    public TestResource (TestDAO testDAO) {
      this.testDAO= testDAO;
    }



    @Path("/list")
    @GET
    public List<TestDto> list() {
        ......


    }
}

我的TestDao.class

public interface TestDao{

    @Mapper(TestMapper.class)
    @SqlQuery (".... ")
    List<Test> list();   
}

1 个答案:

答案 0 :(得分:1)

解决!!

谢谢大家的回答 问题在于guice初始化。 我的代码是:

@Override
    public void initialize(Bootstrap<ServiceConfiguration> bootstrap) {
        super.initialize(bootstrap);
        guiceBundle = GuiceBundle.<ServiceConfiguration>newBuilder()
                  .addModule(new MyBinder())
                  .enableAutoConfig(getClass().getPackage().getName())
                  .setConfigClass(ServiceConfiguration.class)
                  .build();
        bootstrap.addBundle(guiceBundle);
        bootstrap.addBundle(new AssetsBundle("/dist", "/dist", "index.html"));

    }

当我评论这一行时:

.enableAutoConfig(getClass().getPackage().getName()) 

它工作正常。我没有在第一时间发布它,因为我无法想象这可能是邪恶的来源,直到我从头开始创建一个示例项目。