Home > Back-end >  jakarta.servlet with Tomcat 9 - Not Found Issue
jakarta.servlet with Tomcat 9 - Not Found Issue

Time:03-24

I have a application which I am running using eclipse and tomcat 9 server. When I run the application, its opens then index.html page in browser, but the API's called inside index page are erroring with 404 status.

I tried multiple things from various stackoverflow page but nothing seems to work.

I have a Httpservlet with following declaration

@WebServlet("/api/modules")
public class ModuleController extends HttpServlet {
@Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
     // logic
}
}

Even if I directly try to hit localhost:8080/application-context/api/modules I get the same 404 error.

Also my application doesn't have a web.xml.

Note: The application is working fine on other people's system.

Following is the pom.xml for reference.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>ch.bfh.ti</groupId>
    <artifactId>academia-02</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <!-- General properties -->
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <!-- Version properties -->
        <servlet.version>5.0.0</servlet.version>
        <jackson.version>2.13.1</jackson.version>
        <postgresql.version>42.3.2</postgresql.version>
        <jetty.version>11.0.8</jetty.version>
        <h2.version>2.1.210</h2.version>
        <junit.version>5.8.2</junit.version>
        <rest-assured.version>4.5.0</rest-assured.version>

        <!-- Checkstyle properties -->
        <checkstyle.config.location>src/etc/checkstyle.xml</checkstyle.config.location>
        <checkstyle.header.file>src/etc/checkstyle-header.txt</checkstyle.header.file>
    </properties>

    <dependencies>
        <!-- Servlet dependencies -->
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <!-- Database dependency -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgresql.version}</version>
        </dependency>

        <dependency>
            <groupId>com.wordnik</groupId>
            <artifactId>swagger-jaxrs_2.10</artifactId>
            <version>1.3.1</version>
            <scope>compile</scope>
        </dependency>

        <!-- Test dependencies -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>${jetty.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-annotations</artifactId>
            <version>${jetty.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>${h2.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>${rest-assured.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>3.0.0-M5</version>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.11.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>site</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.basedir}/target/site/images</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.basedir}/src/site/markdown/images</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>3.2.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jxr-plugin</artifactId>
                <version>3.1.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
                <version>3.0.0-M5</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>3.1.2</version>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>checkstyle</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
        </plugins>
    </reporting>

</project>

404 error reference:

HTTP Status 404 – Not Found

Type Status Report

Message The requested resource [/academia-02/api/modules] is not available

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Also I have already tried the following:

  • Changing tomcat version
  • adding a web.xml
  • clean build the application and then run on tomcat
  • checked that generated classes contains the Servlet class in proper location
  • Update tomcat properties -> Switch location
  • Update tomcat properties -> user tomcat installation in server location

Can you please let me know what can be done here ?

CodePudding user response:

You're declaring a dependency on Servlet Spec 5.0 (and explicitly state that you're using a Jakarta servlet).

Thus, it's mandatory that you're using Tomcat 10. Works as declared/designed. Not an issue.

  • Related