简体   繁体   中英

Get 404 error and don't know why

Greetings I try to write some streaming webApplication for data so I used an tutorial and started to build my WebApplication, my client, my JS and my HTML document. Now my HTML document has 2 input boxes 1 for a name and 1 for a value if I fill both example (name: DAGO, value:2.2) and then I click on my GET button it saves this data into an HashMap that works so far.

When I try to read the value of DAGO with my GET button it tells me 404 not found ... but the get function is in my WebApp and I don't know why I get this error.

Hope I discriped my problem good enough if not please comment and I will add what you need source code follows now.

JavaScript code

$(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 code(set and get function)

@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 code

<!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>
private MarketPriceStore marketPriceStore = new MarketPriceStore();

The marketPriceStore is a variable of object when object exists, but the object disappear when store function finish call, so marketPriceStore disappears.

You can change the

private MarketPriceStore marketPriceStore = new MarketPriceStore();

to

private static MarketPriceStore marketPriceStore = new MarketPriceStore();

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