jersey icon indicating copy to clipboard operation
jersey copied to clipboard

Unable to integrate Jersey with Jetty Embedded server

Open ashok-mariyala opened this issue 1 year ago • 6 comments

Jetty Versions

jetty-server: 12.0.2
jetty-servlet: 11.0.17
jetty-util: 12.0.2

Jersey versions

jersey-server: 3.1.0-M8
jersey-container-servlet-core: 3.1.0-M8
jersey-container-jetty-http: 3.1.0-M8
jersey-hk2: 3.1.0-M8
jakarta.annotation-api: 3.1.0-M8

Java version/vendor java 17.0.8

OS type/version Mac

Description

try {
	// we created handler without sessions, because REST is stateless.
	var contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);

	// Sets application path to the root.
	contextHandler.setContextPath("/");
	jettyServer.setHandler(contextHandler);

	// Adds Servlet that will handle requests on /api/*
	ServletHolder jerseyServlet = contextHandler.addServlet(ServletContainer.class, "/api/*");
	jerseyServlet.setInitOrder(0);

	// set package where rest resources are located.
	jerseyServlet.setInitParameter("jersey.config.server.provider.packages",
			"com.ashok.gateway.rest.controller");
	jettyServer.start();
	LOG.error("Jetty Server started...!!");
	jettyServer.join();
} catch (Exception e) {
	LOG.error("Exception while starting jetty server. {}", e.getMessage(), e);
	Thread.currentThread().interrupt();

	try {
		jettyServer.stop();
	} catch (Exception ex) {
		LOG.error("Exception while stoping jetty server. {}", ex.getMessage(), ex);
	}
	jettyServer.destroy();
}

I am using above code to handle REST requests. Above code works fine in Jetty 11.x jars. But when i update jetty versions to 12.x above code compilation is failed. Gives following errors.

[ERROR] /src/main/java/com/ashok/gateway/jetty/JettyApiServer.java:[45,69] cannot access jakarta.servlet.http.HttpServlet
[ERROR]   class file for jakarta.servlet.http.HttpServlet not found
[ERROR] /src/main/java/com/ashok/gateway/jetty/JettyApiServer.java:[46,38] cannot access org.eclipse.jetty.server.UserIdentity
[ERROR]   class file for org.eclipse.jetty.server.UserIdentity not found
[ERROR] -> �[1m[Help 1]�[m

ashok-mariyala avatar Oct 11 '23 14:10 ashok-mariyala

First, you should probably not mix Jetty 11 and 12 jars. Second, Jetty 12 would be supported since Jersey 3.1.4.

jansupol avatar Oct 11 '23 14:10 jansupol

@jansupol thank you for your response. I am using the all the latest jetty jars. and I didn't find Jersey 3.1.4 dependencies in the maven repo. Could you please help me any sample examples to develop REST application using Jetty 12 dependencies.

ashok-mariyala avatar Oct 11 '23 15:10 ashok-mariyala

Jersey 3.1.4 is not released, yet.

jansupol avatar Oct 11 '23 15:10 jansupol

@jansupol thanks for your response. Is there any tentative timeline to release 3.1.4? Please let me know. Thanks in advance.

ashok-mariyala avatar Oct 11 '23 15:10 ashok-mariyala

Assumingly 2-3 weeks,

jansupol avatar Oct 11 '23 15:10 jansupol

@ashok-mariyala The Jetty team did a huge refactoring in Jetty 12 and removed the servlet classes from the Jetty core. This allows the use of different servlet versions with the same Jetty core.

The correct new servlet artifact for Jersey 3.1 (Jakarta EE 10) is jetty-ee10-servlet.

zUniQueX avatar Oct 15 '23 11:10 zUniQueX