javamaventomcatjakarta-ee

I deployed WAR on Tomcat but I can't access my controller?


I made a small jakarta ee project, created the war, copied the war file to my tomcat's webapps folder, started it but it does not access my controller method, it throws 404:

package com.example.config;

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;

@ApplicationPath("/api")
public class AppConfig extends Application {
}
package com.example.controller;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

@Path("/hello")
public class BaseController {

    @GET
    public String hello() {
        return "Hello World";
    }
}

mvn compile war:war --> rename the war file to myapp.war --> copy myapp.war to tomcat /webapps --> run the tomcat with startup.bat --> open localhost:8080

I can see that it is running enter image description here

But if I try to access http://localhost:8080/myapp/api/hello

enter image description here

I wonder what am I missing?


Solution

  • It is important to add the relevant implementation of jersey. Because Tomcat 10.1 does not provide a complete implementation of Jakarta EE 10.

            <dependency>
                <groupId>org.glassfish.jersey.core</groupId>
                <artifactId>jersey-server</artifactId>
                <version>3.1.7</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.containers</groupId>
                <artifactId>jersey-container-servlet</artifactId>
                <version>3.1.7</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.inject</groupId>
                <artifactId>jersey-hk2</artifactId>
                <version>3.1.7</version>
            </dependency>
    

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.example</groupId>
        <artifactId>myapp</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>myapp</name>
        <packaging>war</packaging>
        <description>myapp project for Spring Boot</description>
        <properties>
            <maven.compiler.source>17</maven.compiler.source>
            <maven.compiler.target>17</maven.compiler.target>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>jakarta.platform</groupId>
                <artifactId>jakarta.jakartaee-web-api</artifactId>
                <version>10.0.0</version>
                <scope>provided</scope>
            </dependency>
    
            <dependency>
                <groupId>org.glassfish.jersey.core</groupId>
                <artifactId>jersey-server</artifactId>
                <version>3.1.7</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.containers</groupId>
                <artifactId>jersey-container-servlet</artifactId>
                <version>3.1.7</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.inject</groupId>
                <artifactId>jersey-hk2</artifactId>
                <version>3.1.7</version>
            </dependency>
            
        </dependencies>
    
        <build>
            <finalName>myapp</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.4.0</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    Project Tree

    myapp
    ├── pom.xml
    └── src
        └── main
            └── java
                └── com
                    └── example
                        ├── config
                        │   └── AppConfig.java
                        └── controller
                            └── BaseController.java
    

    Build

    mvn clean package
    

    Put war file into Tomcat

    myapp.war

    Test

    curl http://localhost:8080/myapp/api/hello