'why does tomcat show 404 response message for an existing resource?

I'm new to implementing web services and I'm doing it with jax-rs API using eclipse IDE for java-ee developers 2022.

I have written a simple web service that returns a response object including a java object, which has been converted into XML using JAXB, when I run it on tomcat10 a 404 message gets returned. I have heard of some possible causes like mistyping the URI but seems it is not the case.

I have used jersey archetype , artifact: jersey-quickstart-webapp

My project structure.

my project structure

my web.xml file :

    <?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>ir.Institude.BackendCode.Jax-rs</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

my jax-rs service class :

package ir.Institude.BackendCode.Servicers;

import ir.Institude.BackendCode.Entities.UserXmlInformation;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;

@Path("user")
public class UserRequestServicer {
    
    @GET
    @Path("/write")
    @Produces("application/xml")
    public Response UserInformationReciever() {
        
        return Response.ok(new UserXmlInformation()).build();
    }
}

my pom file :

<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>Ir.Institude.BackendCode.Services</groupId>
    <artifactId>Jax-rs</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>Jax-rs</name>

    <build>
        <finalName>Jax-rs</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>16</source>
                    <target>16</target>
                </configuration>
            </plugin>
                        <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.3.1</version>
</plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
        </dependency>
        <dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>3.0.2</version>
</dependency>
        <!-- uncomment this to get JSON support
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-binding</artifactId>
        </dependency>
        -->
    </dependencies>
    <properties>
        <jersey.version>3.0.4</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

tomcat

I don't want to make my question longer than this by pasting so many lines of code. if any other piece of my project is necessary tell me to paste it.

Question Edited.

pom file added, jersey archetype added



Solution 1:[1]

The answer turned out to be not using jersey. I don't really know why and how but forgetting about jersey and starting to use resteasy solved the problem.

also added the resteasy servlet initializer dependency :

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>6.0.0.Final</version>
</dependency>

Solution 2:[2]

The issue :

  1. Please check your port number. Please use localhost:9999 to check if you are getting tomcat home page
  2. "Jax-rs" is that the name of your application, if not, please replace it with your app name
  3. Why do you have webapi in your request url, when its not mentioned in your resource. Your controller is looking for /user/write.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Saurabh Jhunjhunwala