【问题标题】:Get 404 error and don't know why得到404错误,不知道为什么
【发布时间】:2015-08-03 10:54:51
【问题描述】:

问候 我尝试为数据编写一些流式 WebApplication,因此我使用了一个教程并开始构建我的 WebApplication、我的客户端、我的 JS 和我的 HTML 文档。 现在我的 HTML 文档有 2 个输入框,如果我填写两个示例(名称:DAGO,值:2.2),则 1 用于名称和 1 用于值,然后单击我的 GET 按钮,它将这些数据保存到有效的HashMap到目前为止。

当我尝试使用我的 GET 按钮读取 DAGO 的值时,它告诉我 404 not found ... 但 get 函数在我的 WebApp 中,我不知道为什么会出现此错误。

希望我能很好地描述我的问题,如果不是,请发表评论,我将添加您现在需要的源代码。

JavaScript 代码

$(document).ready(function(){
$("#get").click(function(){
    var companyName = $("#companyname").val();

    $.ajax({
        url: "http://localhost:8080/TimeStreamingTestartID-1.0-SNAPSHOT/app/simplestockmarket/" + companyName,
        type: "GET",
        success: function(value){
            $("#marketprice").val(value);
        }
    });
});


$("#set").click(function(){
    var companyName = $("#companyname").val();
    var marketPrice = $("#marketprice").val();

    $.ajax({
        url: "http://localhost:8080/TimeStreamingTestartID-1.0-SNAPSHOT/app/simplestockmarket/" + companyName,
          type: "PUT",
          contentType: "text/plain",
          data: marketPrice,
          success: function(value){
          alert("SUCESS: SET!");
          }
    })
    });
});

Java 代码(设置和获取函数)

@Path("/simplestockmarket")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)

public class SimpleStockMarketResource {

    private MarketPriceStore marketPriceStore = new MarketPriceStore();

    @PUT
    @Path("/{companyname}")
    public void store(@PathParam("companyname") String companyName, Double marketPrice){
        companyName = companyName.toUpperCase();
    marketPriceStore.store(companyName,marketPrice);
}

@GET
@Path("/{companyname}")
public Response retrieve(@PathParam("companyname") String companyName){
    companyName = companyName.toUpperCase();
    Response.ResponseBuilder responseBuilder =Response.status(Response.Status.NOT_FOUND);
    marketPriceStore.retrieve(companyName).ifPresent(
        marketPrice -> responseBuilder.status(Response.Status.OK).entity(marketPrice)
    );
    return responseBuilder.build();
}
}

HTML 代码

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
Firmen Name:<br>
<input type="text" name="firmenname" id="companyname"><br>
Aktien Wert:<br>
<input type="text" name="aktienwert" id="marketprice"><br>

<button type= "btn btn-primary" id= "get">get</button>
<button type= "btn btn-primary" id= "set">set</button>


<script src="jquery-1.11.2.min.js"></script>
<script src="bootstrap.min.js"></script>
<script src="script.js"></script>


</body>
</html>

【问题讨论】:

    标签: javascript java jquery html web-applications


    【解决方案1】:
    private MarketPriceStore marketPriceStore = new MarketPriceStore();
    

    marketPriceStore在object存在时是object的一个变量,但是在store函数调用完成后object就消失了,所以marketPriceStore就消失了。

    你可以改变

    private MarketPriceStore marketPriceStore = new MarketPriceStore();
    

    private static MarketPriceStore marketPriceStore = new MarketPriceStore();
    

    【讨论】:

      猜你喜欢
      • 2017-03-09
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 2014-04-10
      • 2019-11-12
      • 1970-01-01
      • 2016-02-01
      • 2021-04-09
      相关资源
      最近更新 更多