【问题标题】:Attempting to do a Restful Service using Java Eclipse. I am getting an Error 404尝试使用 Java Eclipse 做一个 Restful 服务。我收到错误 404
【发布时间】:2022-07-04 02:15:50
【问题描述】:

我正在尝试使用以下教程创建一个 Restful 服务。因为我使用的是 Tommee plus,所以我的理解是文件路径不需要 web.xml 文件。无论如何,我从 tomee plus 服务器收到错误 404。

我正在使用 java jdk 1.8、Tommee plus 8.0 和 javax.ws.rs-api-2.1.1.jar。日食 2022。

https://www.theserverside.com/video/Step-by-step-RESTful-web-service-example-in-Java-using-Eclipse

预期的路径都不起作用。我不知道为什么。

package com.mcnz.restful.java.example;
import javax.ws.rs.*;

@Path("/")
public class ScoreService {
    public 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("application/plain")
    public int increaseWins() {
        return ++wins;
    }
    @POST @Path("/score/ties")@Produces("application/plain")
    public int increaseTies() {
        return ++ties;
    }
    @POST @Path("/score/losses")@Produces("application/plain")
    public int increaseLosses() {
        return ++losses;
    }
    @GET @Path("/score/wins")@Produces("application/plain")
    public int getWins() {
        return wins;
    }
    @GET @Path("/score/losses")@Produces("application/plain")
    public int getLosses() {
        return losses;
    }
    @GET @Path("/score/ties")@Produces("application/plain")
    public int getTies() {
        return ties;
    }
    
}

【问题讨论】:

  • 你调用的是什么 URL,这个源文件中的什么使它有效?
  • @nitind 所以,我的理解是我的基本 url 应该是:localhost:8080/restful-java 然后,我添加例如“score”来获得:localhost:8080/restful-java/score OR localhost:8080/restful-java/score/wins
  • 地址/端口#对来自我自己的系统和正在运行的服务器,“restful-java”是项目的名称。然后,@Path() 标记添加到 URL。我使用的教程表明 Tomee plus 能够在没有任何 XML 文件的情况下处理此方法。
  • 对,但您实际调用的是哪个 URL?不是示例,而是失败的示例。
  • 我已经调用了所有的 localhost:8080/restful-java/score , localhost:8080/restful-java/score/wins , localhost:8080/restful-java/score/ties, localhost:8080 /restful-java/score/losses 。他们都失败了。

标签: java eclipse rest tomcat


【解决方案1】:

假设所有其他配置都正确,并且您正在从同一物理机/vm 访问 url,请尝试 localhost:8080/score、localhost:8080/score/wins 等。

【讨论】:

    猜你喜欢
    • 2017-02-07
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多