简体   繁体   中英

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

我的 RestFul API 代码图片

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>

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

我的RestFul API代码图片

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>

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

我的RestFul API代码图片

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>

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

我的RestFul API代码图片

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>

If someone search the solution for the same problem in 2021 so the solution are:

-Install latest version of Eclipse 2021 -Install and use Tomcat 10

For me i used Eclipse 2021 with tomcat 9 and i waste 4 hours and did not find the solution and finally by installing the tomcat 10 that is supported in Eclipse 2021 my problem solved.

Again if you have this error there is not big problem with you code because in my case even i had error with template demo code.

I hope it is helpful good coding and coffee !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM