0

Trying to do a Hello World with Jersey and Tomcat 10 using IntelliJ as IDE. Unfortunately, I'll get a 404 - it seems the servlet is not loaded. Reading several tutorials, I can't find the reason. Also I'm getting a weird error message in my web.xml...

pom.xml

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>8.0.1</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>

web.xml (xml-file is mandatory, I can't use the ApplicationConfig-class)

    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.myapp.hello</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

Also, here I'm getting this error message on the servlet-class line: 'org.glassfish.jersey.servlet.ServletContainer' is not assignable to 'javax.servlet.Servlet,jakarta.servlet.Servlet' I only found the https://youtrack.jetbrains.com/issue/IDEA-86833 issue, but it seems to be resolved.

Controller/Endpoint

package com.myapp.hello;
@Path("/hello")
public class DataController  {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
        return "Hello Jersey Plain";
    }
}

This should deploy the servlet and make it accessable under through a GET request with the following url: http://localhost:8080/app/api/hello - but getting a 404 instead...

What am I doing wrong here? Code seems fine, I don't see anything in the server logs.

Thanks for any inputs!

Remo
  • 1,112
  • 2
  • 12
  • 25
  • 1
    Jersey 3.x is for Servlet 5.0 (Tomcat 10) but you seem to be using something Servlet 3.1 based (Tomcat 8). Either downgrade Jersey to 2.x or upgrade Tomcat to 10.x. See currently accepted answer of abovelinked dupe. – BalusC Feb 15 '21 at 13:31
  • @BalusC The error message sounds like it would be from Tomcat 10 as it's aware of the Jakarta namespace. The message doesn't make sense though as the ServletContainer (in 3.0.1) does in fact extends the Jakarta HttpServlet. – Paul Samsotha Feb 15 '21 at 19:51
  • @Remo 1. Can you post the complete stack trace? 2. Why are you including the javaee dependency? 3. Why are you including servlet 3.1 dependency? 4. What version of Tomcat are you using? – Paul Samsotha Feb 15 '21 at 20:03
  • @PaulSamsotha 1. There is no stacktrace. 2/3. How would I be able to use the Annotations and stuff from javax.ws etc.? 4. Tomcat 10.0.2 – Remo Feb 16 '21 at 12:43
  • btw it works with Jersey 2.x, but as soon as I use Jersey 3, I'm getting the error in the web.xml shown above. – Remo Feb 16 '21 at 12:44
  • 1
    Your pom.xml is utterly wrong for Tomcat 10. You're basically trying to convert it to Tomcat 8.0. In the abovelinked duplicate, head to the "See also" section in bottom of the answer and click the 2nd link. It will show you proper pom.xml configuration for Tomcat 10 and explain in text how you should actually properly declare dependencies for a non-JEE container such as Tomcat. – BalusC Feb 16 '21 at 12:53

0 Answers0