Reputation: 55
I'm trying to write a REST application with Jersey and Jetty. Stack: Java 11, Gradle 6.1.1, Jersey 2.30, Hibernate 5.4.X, Jetty server and servlet in 9.4.26.v20200117 version. The problem appears when HK2 is injecting the dependencies. Here is a error log:
Feb 11, 2020 10:49:41 PM org.glassfish.jersey.internal.Errors logErrors
WARNING: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 2
java.lang.ExceptionInInitializerError
...
Here is Jetty main class:
package com.familybank;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.servlet.ServletContainer;
public class Application {
public static void main(String[] args) {
// Jersey
final var servletContextHandler = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
final var jerseyServletContainer = new ServletContainer(new ApplicationResourceConfiguration());
final var jerseyServletHolder = new ServletHolder(jerseyServletContainer);
servletContextHandler.setContextPath("/");
servletContextHandler.addServlet(jerseyServletHolder, "/api/*");
// Wire up Jetty
final var handlerList = new HandlerCollection();
handlerList.setHandlers(new Handler[]{ servletContextHandler });
final var server = new Server(8080);
server.setHandler(handlerList);
/*
final var uri = UriBuilder.fromUri("http://localhost/").port(8080).build();
final var server = JettyHttpContainerFactory.createServer(uri, new ApplicationResourceConfiguration());
*/
try {
server.start();
server.join();
} catch (Exception ex) {
// logger.error("Error occurred while starting Jetty", ex);
System.exit(1);
} finally {
server.destroy();
}
}
}
Also ApplicationBinder:
public class ApplicationBinder extends AbstractBinder {
@Override
protected void configure() {
// DAO dependency injection
bind(UserDAO.class).to(UserDAO.class);
bind(PasswordDAO.class).to(PasswordDAO.class);
bind(RoleDAO.class).to(RoleDAO.class);
// Service dependency injection
bind(UserServiceImpl.class).to(UserService.class);
}
}
And the ApplicationResourceConfiguration:
public class ApplicationResourceConfiguration extends ResourceConfig {
public ApplicationResourceConfiguration() {
// register(UserController.class); // With or without the result is the same
register(new ApplicationBinder());
packages(true, "com.example");
// register(UserController.class);
}
}
Additionally UserController looks like:
@Path("/user")
public class UserController {
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
private final UserService userService;
@Inject
public UserController(UserService userService) {
this.userService = userService;
}
@GET
@Path("/get-all")
@Produces(MediaType.APPLICATION_JSON)
public User getAll() {
logger.debug("GET /user/get-all");
return userService.getByLogin("[email protected]").orElseGet(User::new);
}
}
UserServiceImpl using a RoleDAO, PasswordDAO and UserDAO. DAO classes use EntityManagerFactory to get into database.
EntityManagerFactoryProvider class:
public class EntityManagerFactoryProvider {
private static EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("family-bank");
public static EntityManagerFactory getEntityManagerFactory() {
return entityManagerFactory;
}
}
Project also contains persistance.xml configuration - it works in other project so should be fine.
The most strange thing is that when I run project from IntelliJ it works well. Jar building and run with no actions also looks good, but the stairs appear when I'm calling url from web-browser: http://localhost:8080/api/user/get-all
Then HK2 failed with dependency injection. I have no idea what is wrong. If someone need I can send whole stack trace of exceptions (didn't put it there, cause of its length...).
How to configure Jersey 2 and Jetty stuff to work in described case?
Upvotes: 0
Views: 2249