0

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

Navid Nasro
  • 19
  • 1
  • 6
  • How are you trying to access the application – Saurabh Jhunjhunwala Mar 30 '22 at 19:59
  • @SaurabhJhunjhunwala what do you mean? I run it on a tomcat server and enter the URI which is specified to be handled by a method. but a 404 message gets returned and I can't find out why. – Navid Nasro Mar 31 '22 at 06:08
  • Please share the request url, 404 comes if your url is incorrect – Saurabh Jhunjhunwala Mar 31 '22 at 06:59
  • @SaurabhJhunjhunwala I have added it at the bottom of my question. it's a picture that shows the 404 message and the URI I entered. I named it tomcat.it is a link. – Navid Nasro Mar 31 '22 at 08:27
  • Which version of Jersey are you using? Is it compatible with Tomcat 10 and other Jakarta Servlet containers, or only older versions that are Java Servlet containers? – nitind Mar 31 '22 at 12:46
  • 1
    ir.Institude.BackendCode.Jax-rs does seem wrong. Maybe you wanted ir.Institude.BackendCode.Servicers? – jan.supol Mar 31 '22 at 13:44
  • @nitind I just uploaded the code in my pom file, the jersey version is 3.0.4. I can't tell whether it is compatible or not because I'm not that much familiar with the structure and components of a maven project. – Navid Nasro Mar 31 '22 at 18:35
  • @jan.supol No it was generated by eclipse. by the way I tested what you said but again, no luck! – Navid Nasro Mar 31 '22 at 18:40

2 Answers2

0

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.
Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
  • 1. I ran the project on server and entered the URI localhost:9999 and 404 showed up. 2."Jax-rs" is the name of the project and is the artifact 3. the webapi has been set as the uri-pattern of servelet as /webapi/* .i used an archtype for the creation of project and this was generated automatically. – Navid Nasro Mar 31 '22 at 10:14
  • as i said i'm new to web services and not familiar with the structure of web service projects.like I don't know much about pom or web XML files and how much impact they have on the project. I know how to code for web services but unfortunately, I don't know how to instruct my project. – Navid Nasro Mar 31 '22 at 10:18
0

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>
Navid Nasro
  • 19
  • 1
  • 6