Skip to content
Advertisement

Tomcat 404 – Not Found: when Implementing REST API

I’m trying to implement simple RESTFUL API service using dynamic web application in Eclipse. Every time I am getting error message HTTP Status 404 – Not Found.

I have attached my screen below

Package structure

My RestFul API Code Picture

I am able to run tomcat server on localhost:8080 as shown on the above figure but when I trying to access my api path I am getting 404 not found error

Here is my whole code

package com.restful.java.example;

import javax.ws.rs.*;

@Path("/")
public class ScoreService {
    private static int wins ,losses, ties;

    @GET
    @Path("/score")
    @Produces("application/json")
    public String getScore() {
       String pattern = 
          "{ "wins":"%s", "losses":"%s", "ties": "%s"}";
       return String.format(pattern,  wins, losses, ties );   
    }
     //localhost:8080/restful-java/score?wins=2%losses=3@ties=15
    @PUT
    @Path("/score")
    @Produces("application/json")
    public String updateScore( @QueryParam("wins")int wins,
                                @QueryParam("losses")int losses,
                                @QueryParam("ties")int ties) {
        ScoreService.wins = wins;
        ScoreService.losses = losses;
        ScoreService.ties = ties;
        String pattern = 
                  "{ "wins":"%s", "losses":"%s", "ties": "%s"}";
              return String.format(pattern,  wins, losses, ties );   


    }

    @POST @Path("/score/wins") @Produces("text/plain")
    public int increaseWins() {
        return ++wins;
    }
    @POST @Path("/score/ties") @Produces("text/plain")
    public int increaseTies() {
        return ++ties;
    }
    @POST @Path("/score/losses") @Produces("text/plain")
    public int increaseLosses() {
        return ++losses;
    }

    @GET @Path("/score/wins") @Produces("text/plain")
    public int getWins() {
        return wins;
    }

    @GET @Path("/score/losses") @Produces("text/plain")
    public int getLosses() {
        return losses;
    }

    @GET @Path("/score/ties") @Produces("text/plain")
    public int getTies() {
        return ties;
    }
}

Inside Webcontent I have web.xml and I have following code here:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>restful-java</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Advertisement

Answer

You have missed the mapping in your web.xml. Add the following lines :

<servlet>
    <servlet-name>My Servlet</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.restful.java.example</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>My Servlet</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

Basically what this configuration is doing is mapping urls with pattern /api/* to a servlet which is required.

Also looks like you are using jersey for your REST implementation. Now you can try to access your api’s using the following url:

http://localhost:8080/restful-java/api/score

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement