404 when requested using Postman to Restful endpoint



  • I created a dynamic web project with these classes:

    StockQuoteApplication

    package lesson33;
    

    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;

    @ApplicationPath("resources")
    public class StockQuoteApplication extends Application {
    }

    Class Stock:

    package lesson33.service;

    import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement
    public class Stock {
    private String symbol;
    private Double price;
    private String currency;
    private String country;

    public Stock() {
    }
    
    public Stock(String symbol, Double price, String currency, String country) {
        this.symbol = symbol;
        this.price = price;
        this.currency = currency;
        this.country = country;
    }
    
    public String getSymbol() {
        return symbol;
    }
    
    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }
    
    public Double getPrice() {
        return price;
    }
    
    public void setPrice(Double price) {
        this.price = price;
    }
    
    public String getCurrency() {
        return currency;
    }
    
    public void setCurrency(String currency) {
        this.currency = currency;
    }
    
    public String getCountry() {
        return country;
    }
    
    public void setCountry(String country) {
        this.country = country;
    }
    

    }

    Class StockService:

    package lesson33.service;

    import javax.ws.rs.Consumes;
    import javax.ws.rs.FormParam;
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.Response;

    @Path("/stock")
    public class StockService {

    @Produces({"application/xml","application/json"})
    @Path("{symbol}")
    @GET
    public Stock getStock(@PathParam("symbol") String symbol) {
    
        Stock stock = StockServiceHelper.getStock(symbol);
    
        if (stock == null) {
            return new Stock("NOT FOUND", 0.0, "--", "--");
        }
    
        return stock;
    }
    
    @POST
    @Consumes("application/x-www-form-urlencoded")
    public Response addStock(@FormParam("symbol") String symbol,
                             @FormParam("currency") String currency,
                             @FormParam("price") String price,
                             @FormParam("country") String country) {
    
        if (StockServiceHelper.getStock(symbol) != null)
            return Response.status(Response.Status.BAD_REQUEST).
                    entity("Stock " + symbol + " already exists").type("text/plain").build();
    
        double priceToUse;
        try {
            priceToUse = new Double(price);
        }
        catch (NumberFormatException e) {
            priceToUse = 0.0;
        }
    
        StockServiceHelper.addStock(new Stock(symbol, priceToUse, currency, country));
    
        return Response.ok().build();
    }
    

    }

    Class StockServiceHelper:

    package lesson33.service;

    import java.util.HashMap;
    import java.util.Map;

    public class StockServiceHelper {
    public static void addStock(Stock stock) {
    stocks.put(stock.getSymbol(), stock);
    }

    public static void removeStock(String symbol) {
        stocks.remove(symbol);
    }
    
    public static Stock getStock(String symbol) {
        return stocks.get(symbol);
    }
    
    private static Map<String, Stock> stocks = new HashMap<>();
    
    static {
        generateStocks();
    }
    
    private static void generateStocks() {
        addStock(new Stock("IBM", 43.12, "USD", "USA"));
        addStock(new Stock("AAPL", 120.0, "USD", "USA"));
     }
    

    }

    I put a project on glassfish4.1. Then I'm trying to connect to him with the postman reference. http://localhost:8080/Lesson33/resources/IBM using GET. Name of my project Lesson33. Issued HTTP Status 404 - Not Found

    Help me with the problem. And please explain where you can see the logic of how I try to ask. Not the logic of glassfish, but others, how to track where the error occurred, malfunction.



  • lesson33 Not specified and yet to be indicated stock♪ Anyway,

    http://localhost:8080/resources/stock/IBM 
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2