Sid111Math
Sid111Math

Reputation: 157

Tomcat Couldn't find Webservice 404

I am creating a simple webservice project that pulls data from db on countries and displays it in a JSON form, My project does not throw any error and builds successfully but it is unable to find deployed resource.

WorldInformation.java class

package com.webservices;

import java.util.List;

import com.webservices.models.Country;
import com.webservices.services.WorldInformationService;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/worldinformation")
public class WorldInformation {

    WorldInformationService  worldInformationService = new WorldInformationService();

    @GET
    @Path("/getCountries")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Country> getCountries(){
        System.out.println("reached point 1");
        List<Country> countryList = worldInformationService.getCountries();
        return countryList;
    }

    @POST
    @Path("/setCountry/{country}/{countryCode}")
    @Consumes(MediaType.TEXT_PLAIN)
    public void setCountry(@PathParam("country") String country,@PathParam("countryCode") String countryCode){
        worldInformationService.setCountry(country,countryCode);
    }

}

This is 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>com.webservices</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

And this is what pom.xml looks like

<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>com.webservices</groupId>
    <artifactId>WorldInformation</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>WorldInformation</name>


    <build>
        <finalName>WorldInformation</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </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>
        <!-- uncomment this to get JSON support
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-binding</artifactId>
        </dependency>
        -->
          <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.10.Final</version>
</dependency>
  <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.19</version>
</dependency>
    </dependencies>
    <properties>
        <jersey.version>3.0.0-M1</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

This is error snapshot enter image description here

I can't seem to figure out how to get this resolved and what's going wrong, will deeply appreciate your help on this.

Upvotes: 1

Views: 360

Answers (2)

Sid111Math
Sid111Math

Reputation: 157

Thanks for your valuable feedbacks, here is what was worked for me. As someone suggested. I was not looking at correct point. I was missing application name in the url I was trying to connect to. Though I ran into other problems but I managed to solve them and now its working for me.

localhost:8080/WorldInformation/worldinformation/getCountries

Thanks for your valuable help on this.

Upvotes: 0

Anish B.
Anish B.

Reputation: 16469

I think you need to add @ApplicationPath in your jax-rs application as well.

Note: The URL is also missing the context path.

For example: http://localhost:8080/<context-path>/servlet/path

From Oracle Docs :

The @ApplicationPath annotation is used to define the URL mapping for the total application. The path specified by @ApplicationPath is the base URI for all resource URIs specified by @Path annotations in the resource class.

For example : Add a class WorldInformationApp annotated with @ApplicationPath in the project.

@ApplicationPath("/worldinfo")
public class WorldInformationApp extends Application {

}

Try to access the resource with this url : http://localhost:8080/<context-path>/worldinfo/worldinformation/getCountries

Note : I think you are using wrong libraries for jax-rs app.

For example : javax.ws.rs.* should be used instead of jakarta.ws.rs.*

Upvotes: 1

Related Questions