Как использовать инъекцию Guice Dependency с каркасом vertx-джерси - PullRequest
0 голосов
/ 14 февраля 2019

Что я пробовал до сих пор

public class Main {

  private final static Logger LOGGER = Logger.getLogger(Main.class.getName());

  public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();
    vertx.runOnContext(aVoid -> {

      // Set up the jersey configuration
      // The minimum config required is a package to inspect for JAX-RS endpoints
      vertx.getOrCreateContext().config()
          .put("jersey", new JsonObject()
              .put("port", 8080)
              .put("features", new JsonArray()
                  .add("org.glassfish.jersey.server.mvc.freemarker.FreemarkerMvcFeature"))
              .put("guice_binder", Binder.class.getName())
              .put("packages", new JsonArray()
                  .add(EComAppController.class.getPackage().getName())));

      // Use a service locator (HK2 or Guice are supported by default) to create the jersey server
      ServiceLocator locator = ServiceLocatorUtilities
          .bind(new HK2JerseyBinder(), new HK2VertxBinder(vertx));
      JerseyServer server = locator.getService(JerseyServer.class);
      // Start the server which simply returns "Hello World!" to each GET request.
      server.start();
      LOGGER.info("Server started successfully");
    });
  }

}

Джерси Ресурс

@Path("/")
public class EComAppController {

  private static final String CDN_URL = "CDN_URL";

  private EComAppUtilInterface eComAppUtil;
  @Inject
  public EComAppController(EComAppUtilInterface eComAppUtil) {
    this.eComAppUtil = eComAppUtil;
  }


  @GET
  @Produces(MediaType.TEXT_HTML)
  public Viewable doGet(@Context Vertx vertx) {
    final Map<String, Object> map = new HashMap<String, Object>();
    map.put(CDN_URL, eComAppUtil.getEComCDNUrlForImages());
    return new Viewable("/templates/index.ftl", map);

  }

  @GET
  @Produces(MediaType.TEXT_HTML)
  @Path("/test")
  public String doTest(@Context Vertx vertx) {

    return "I love java";

  }

}

EComAppUtil.class

public class EComAppUtil implements EComAppUtilInterface{

  public String getEComCDNUrlForImages() {
    return "http://localhost:8081/images/";
  }

}

Binder

public class Binder extends AbstractModule {

  @Override
  protected void configure() {
    bind(EComAppUtilInterface.class).to(EComAppUtil.class).in(Singleton.class);

  }
}

При нажатии на URL: http://localhost:8080/

Я получаю ниже ошибки

   SEVERE: A MultiException has 1 exceptions.  They are:
    1. java.lang.NoSuchMethodException: Could not find a suitable constructor in com.rt.controller.EComAppController class.

    MultiException stack 1 of 1
    java.lang.NoSuchMethodException: Could not find a suitable constructor in com.rt.controller.EComAppController class.
        at org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getConstructor(JerseyClassAnalyzer.java:192)
        at org.jvnet.hk2.internal.Utilities.getConstructor(Utilities.java:178)
        at org.jvnet.hk2.internal.Utilities.justCreate(Utilities.java:990)
        at org.jvnet.hk2.internal.ServiceLocatorImpl.create(ServiceLocatorImpl.java:975)
        at org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:1067)
        at org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:1059)
        at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:173)
        at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:284)
        at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:74)
        at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:109)
        at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)
        at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)
        at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)
        at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:92)
        at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:61)
        at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
        at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:318)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
        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.process(Errors.java:267)
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
        at com.englishtown.vertx.jersey.impl.DefaultApplicationHandlerDelegate.handle(DefaultApplicationHandlerDelegate.java:49)
        at com.englishtown.vertx.jersey.impl.DefaultJerseyHandler.handle(DefaultJerseyHandler.java:270)
        at com.englishtown.vertx.jersey.impl.DefaultJerseyHandler.handle(DefaultJerseyHandler.java:176)
        at com.englishtown.vertx.jersey.impl.DefaultJerseyHandler.handle(DefaultJerseyHandler.java:151)
        at com.englishtown.vertx.jersey.impl.DefaultJerseyHandler.handle(DefaultJerseyHandler.java:61)
        at io.vertx.core.http.impl.Http1xServerConnection.processMessage(Http1xServerConnection.java:453)
        at io.vertx.core.http.impl.Http1xServerConnection.handleMessage(Http1xServerConnection.java:144)
        at io.vertx.core.http.impl.HttpServerImpl$ServerHandlerWithWebSockets.handleMessage(HttpServerImpl.java:666)
        at io.vertx.core.http.impl.HttpServerImpl$ServerHandlerWithWebSockets.handleMessage(HttpServerImpl.java:619)
        at io.vertx.core.net.impl.VertxHandler.lambda$channelRead$1(VertxHandler.java:146)
        at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:337)
        at io.vertx.core.impl.ContextImpl.executeFromIO(ContextImpl.java:195)
        at io.vertx.core.net.impl.VertxHandler.channelRead(VertxHandler.java:144)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.vertx.core.http.impl.HttpServerImpl$Http2UpgradeHandler.channelRead(HttpServerImpl.java:984)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:310)
        at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:284)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.vertx.core.http.impl.Http1xOrH2CHandler.end(Http1xOrH2CHandler.java:61)
        at io.vertx.core.http.impl.Http1xOrH2CHandler.channelRead(Http1xOrH2CHandler.java:38)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1359)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:935)
        at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:141)
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:645)
        at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:580)
        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:497)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:459)
        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:886)
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
        at java.lang.Thread.run(Thread.java:748)

Feb 14, 2019 8:47:43 PM org.glassfish.jersey.internal.Errors logErrors
WARNING: The following warnings have been detected: WARNING: HK2 service reification failed for [com.rt.controller.EComAppController] with an exception:
MultiException stack 1 of 2
java.lang.NoSuchMethodException: Could not find a suitable constructor in com.rt.controller.EComAppController class.
    at org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getConstructor(JerseyClassAnalyzer.java:192)
    at org.jvnet.hk2.internal.Utilities.getConstructor(Utilities.java:178)
    at org.jvnet.hk2.internal.ClazzCreator.initialize(ClazzCreator.java:129)
    at org.jvnet.hk2.internal.ClazzCreator.initialize(ClazzCreator.java:180)
    at org.jvnet.hk2.internal.SystemDescriptor.internalReify(SystemDescriptor.java:740)
...