I am trying to use vertx jersey framework along with Spring. I am trying to inject a Spring created bean with the Jersey annotation to be used by the vertx jersey framework, but I am facing a problem with the injection.
Here is my Main code
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");
});
}
}
I am getting the following exception using the above code
SEVERE: Failed to start the jersey container
java.lang.ClassCastException: com.rt.controller.EComAppController cannot be cast to java.lang.CharSequence
Follow "Tell Don't Ask" principle.
Instead of passing EComAppController
(which Vert.x tries to serialize), consider wrapping this bean in a verticle, and communicate with it using EventBus:
class EcomVerticle extends AbstractVerticle {
EComAppController eComAppController;
@Override
public void start() {
ApplicationContext context = new ClassPathXmlApplicationContext("application_context.xml");
this.eComAppController = (EComAppController) context.getBean("ecomAppController");
vertx.eventBus().consumer("ecom-question", (message) -> {
// eComAppController does something with the message
});
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.