簡體   English   中英

Tomcat 404 – 未找到:實施 REST API 時

[英]Tomcat 404 – Not Found: when Implementing REST API

我正在嘗試使用 Eclipse 中的動態 Web 應用程序實現簡單的 RESTFUL API 服務。 每次我收到錯誤消息HTTP Status 404 – Not Found

我在下面附上了我的屏幕

封裝結構

我的 RestFul API 代碼圖片

我能夠在localhost:8080上運行 tomcat 服務器,如上圖所示,但是當我嘗試訪問我的 api 路徑時,我收到404 not found error

這是我的全部代碼

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;
    }
}

Webcontent我有web.xml ,這里有以下代碼:

 <?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>

我正在嘗試使用Eclipse中的動態Web應用程序來實現簡單的RESTFUL API服務。 每次收到錯誤消息HTTP Status 404 – Not Found

我已在下方附上我的屏幕

包裝結構

我的RestFul API代碼圖片

如上圖所示,我能夠在localhost:8080上運行tomcat服務器,但是當我嘗試訪問api路徑時卻遇到404 not found error

這是我的整個代碼

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;
    }
}

Webcontent我有web.xml ,在這里有以下代碼:

 <?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>

我正在嘗試使用Eclipse中的動態Web應用程序來實現簡單的RESTFUL API服務。 每次收到錯誤消息HTTP Status 404 – Not Found

我已在下方附上我的屏幕

包裝結構

我的RestFul API代碼圖片

如上圖所示,我能夠在localhost:8080上運行tomcat服務器,但是當我嘗試訪問api路徑時卻遇到404 not found error

這是我的整個代碼

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;
    }
}

Webcontent我有web.xml ,在這里有以下代碼:

 <?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>

我正在嘗試使用Eclipse中的動態Web應用程序來實現簡單的RESTFUL API服務。 每次收到錯誤消息HTTP Status 404 – Not Found

我已在下方附上我的屏幕

包裝結構

我的RestFul API代碼圖片

如上圖所示,我能夠在localhost:8080上運行tomcat服務器,但是當我嘗試訪問api路徑時卻遇到404 not found error

這是我的整個代碼

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;
    }
}

Webcontent我有web.xml ,在這里有以下代碼:

 <?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>

如果有人在 2021 年搜索相同問題的解決方案,那么解決方案是:

- 安裝最新版本的 Eclipse 2021 - 安裝並使用 Tomcat 10

對我來說,我將 Eclipse 2021 與 tomcat 9 一起使用,我浪費了 4 個小時,但沒有找到解決方案,最后通過安裝 Eclipse 2021 支持的 tomcat 10,我的問題解決了。

同樣,如果您遇到此錯誤,則您的代碼沒有大問題,因為在我的情況下,即使我的模板演示代碼也有錯誤。

我希望它對良好的編碼和咖啡有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM