如何在vertx jersey框架中使用Guice依赖注入

时间:2019-02-14 15:22:14

标签: java guice jersey-2.0 vert.x

到目前为止我尝试过的

java Classname arg1 arg2 arg3

Jersey资源

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");
    });
  }

}

EComAppUtil.class

@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";

  }

}

活页夹

public class EComAppUtil implements EComAppUtilInterface{

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

}

点击网址时:http://localhost:8080/

我遇到错误

public class Binder extends AbstractModule {

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

  }
}

0 个答案:

没有答案