I am new in jakarta EE /webservice. While running on Tomcat 9 server with the following resource, I am getting error java.lang.ClassNotFoundExceptio
java.lang.ClassNotFoundException: jakarta.servlet.Filter
This class is part of Servlet API version 5.0 which in turn is part of Jakarta EE version 9.
And,
<jersey.version>3.0.0-M6</jersey.version>
this Jersey version is based off JAX-RS API version 3.0 which in turn is part of Jakarta EE version 9.
And,
Tomcat 9 server
this Tomcat version is based off Servlet API version 4.0 which in turn is part of Java/Jakarta EE version 8.
So, summarized, the targeted JEE versions don't match up and it's causing trouble for you. You have 2 options:
Downgrade Jersey to version 2.x. This one is based off JAX-RS API version 2.x which in turn is part of Java/Jakarta EE version 8.
Or, upgrade Tomcat to version 10.x. This one is based off Servlet API version 5.0 which in turn is part of Jakarta EE version 9 (which in turn offers JAX-RS API version 3.x for which you can thus use the intended Jersey version).
The technical reason is that during the step from Java/Jakarta EE 8 to Jakarta EE 9 all javax.*
packages have been renamed to jakarta.*
packages. So there is no backwards compatibility anymore since Jakarta EE 9. The target runtime itself (thus, Tomcat itself), has really to be the one targeted for the APIs in use by Jakarta EE 9.
Just changing the jersey version in pom.xml file from <jersey.version>3.0.0-M6</jersey.version> to <jersey.version>3.0.0-M1</jersey.version> worked for me. I think the latest version M6 is not compatible with Java EE (eclipse). Its better to use second latest version meanwhile.
Also worked on Jersey project on Tomcat 9 Try with these dependencies
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.31</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.31</version>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.0.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.31</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-api</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-locator</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-utils</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-sse</artifactId>
<version>2.31</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
<version>2.31</version>
</dependency>
Also, you don't have to put anything in your web.xml as long as you use a class like following
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("jerseyontomcat/*")
public class RestApp extends Application
{
@Override
public Set<Class<?>> getClasses()
{
HashSet<Class<?>> classes = new HashSet<Class<?>>();
classes.add(MyClass.class);
return classes;
}
}
Here would be the contents of web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>JerseyTomcatTest</display-name>
</web-app>