如何将Spring bean注入vertx jersey实例

时间:2019-02-13 16:48:17

标签: java spring vert.x

我正在尝试将vertx jersey框架与Spring一起使用。我正在尝试注入一个带有Jersey注释的Spring创建的bean,以供vertx jersey框架使用,但注入时遇到了问题。

这是我的主要代码

public class Main {

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

  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("application_context.xml");
    EComAppController eComAppController = (EComAppController) context.getBean("ecomAppController");
    List<Object> instances = new ArrayList<>();
    instances.add(eComAppController);
    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("packages", new JsonArray() //This is working, but then vertx-jersey framework will initialize new object which I dont want.
                  .add(eComAppController.getClass().getPackage().getName()))*/
              .put("instances", instances)//This approach not working
          );

      // 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);
      JerseyOptions options = server.getHandler().getContainer().getOptions();
      //options.getInstances().add(eComAppController);
      // Start the server which simply returns "Hello World!" to each GET request.
      server.start();
      //Getting exception on above line
      LOGGER.info("Server started successfully");
    });

  }
}

使用上述代码,我得到了以下异常

SEVERE: Failed to start the jersey container
java.lang.ClassCastException: com.rt.controller.EComAppController cannot be cast to java.lang.CharSequence

1 个答案:

答案 0 :(得分:0)

遵循“不要问”的原则。
与其传递var ul = document.getElementById("ul"); var names = ["Lars", "Peter", "Jan", "Ian"]; names.push = function() { Array.prototype.push.apply(this, arguments); // ... this will execute on each .push var newLi = document.createElement("li"); newLi.appendChild(document.createTextNode(arguments[0])); ul.appendChild(newLi); }; (Vert.x尝试序列化),不如考虑将此bean封装在一个verticle中,并使用EventBus与之通信:

EComAppController