gpt4 book ai didi

休息服务 - Tomcat 能够启动,主页工作正常但不能执行任何 GET

转载 作者:行者123 更新时间:2023-11-29 00:01:27 25 4
gpt4 key购买 nike

` https://www.pegaxchange.com/2016/08/11/jax-rs-java-rest-service-eclipse-tomcat/

我试图通过本指南建立工作休息服务。到目前为止,我还在挣扎。

我可以通过 http://localhost:8080 查看 Tomcat 主页但是看不到任何 GET 搜索等。

试图通过以下 URL 获取 http://localhost:8080/SampleWebProject/search?name= http://localhost:8080/SampleWebProject/productcatalog/search?name= http://localhost:8080/SampleWebProject/myRestServices/search?name=

我仍然收到 404

类型状态报告

消息/SampleWebProject/搜索

描述源服务器未找到目标资源的当前表示或不愿透露存在的表示。

我做错了什么?


尝试从头开始构建它 4 次,在不同的 PC 上,双重检查库,使用 Postman 和 Insomnia。

package com.pegaxchange.java.web.rest;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;

@ApplicationPath("restservices")
public class MyRESTServices extends ResourceConfig {
public MyRESTServices() {
packages("com.fasterxml.jackson.jaxrs.json");
packages("com.pegaxchange.java.web.rest");
}
}



----------


package com.pegaxchange.java.web.rest;

import java.util.*;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
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.QueryParam;
import javax.ws.rs.core.MediaType;

import com.pegaxchange.java.bean.Product;
import com.pegaxchange.java.bean.Status;
@Path("productcatalog")
public class ProductCatalogResource {
private static List<Product> productCatalog;
public ProductCatalogResource() {
initializeProductCatalog();
}
@GET
@Path("search")
@Produces(MediaType.APPLICATION_JSON)
public Product[] searchByName(@QueryParam("name") String name) {
List<Product> products = new ArrayList<Product>();
for (Product p : productCatalog) {
if (name != null && p.getName().toLowerCase().startsWith(name.toLowerCase())) {
products.add(p);
}
}
return products.toArray(new Product[products.size()]);
}

@POST
@Path("insert")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Status insert(Product product) {
productCatalog.add(product);
return new Status("SUCCESS", "Inserted " + product.getName());
}

@GET
@Path("getId")
@Produces(MediaType.APPLICATION_JSON)
public Product get(@QueryParam("id") int id) {
Product product = null;

for (Product p: productCatalog) {
if (id == p.getId() ) {
product = p;
break;
}
}

return product;
}

@POST
@Path("update")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Status update(Product product) {
Status stat = new Status("ERROR", "No ID " + product.getId());

Product prod = get(product.getId());

if (prod != null) {
if (product.getName() != null) {
prod.setName(product.getName());
}
if (product.getUnitPrice() != null) {
prod.setUnitPrice(product.getUnitPrice());
}
stat = new Status("Success", "updated ID " + product.getId());
}
return stat;
}

@DELETE
@Path("delete")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Status delete(@QueryParam("id") int id) {
Status stat = new Status("ERROR", "No ID " + id);

Product product = get(id);

if (product != null) {
productCatalog.remove(product);
stat = new Status("Success", "deleted" + product.getId());
}
return stat;
}

private void initializeProductCatalog() {
if (productCatalog == null) {
productCatalog = new ArrayList<Product>();
productCatalog.add(new Product(1, "Keyboard", 29.99D));
productCatalog.add(new Product(2, "Mouse", 9.95D));
productCatalog.add(new Product(3, "17\" Monitor", 159.49D));
productCatalog.add(new Product(4, "Hammer", 9.95D));
productCatalog.add(new Product(5, "Screwdriver", 7.95D));
productCatalog.add(new Product(6, "English Dictionary", 11.39D));
productCatalog.add(new Product(7, "A House in Bali", 15.99D));
productCatalog.add(new Product(8, "An Alaskan Odyssey", 799.99D));
productCatalog.add(new Product(9, "LCD Projector", 1199.19D));
productCatalog.add(new Product(10, "Smart Thermostat", 1199.19D));
}
}
}


---

package com.pegaxchange.java.bean;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Status implements Serializable {
private static final long serialVersionUID = -9130603850117689481L;
private String status;
private String message;

public Status() {} // needed for JAXB

public Status(String status, String message) {
this.status = status;
this.message = message;
}

public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}

--

package com.pegaxchange.java.bean;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Product implements Serializable {
private static final long serialVersionUID = 6826191735682596960L;
private int id;
private String name;
private Double unitPrice;

public Product() {} // needed for JAXB
public Product(int id, String name, double unitPrice) {
this.id = id;
this.name = name;
this.unitPrice = unitPrice;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getUnitPrice() {
return unitPrice;
}`enter code here`
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
}

最佳答案

就在我发布之后,我创建了一个解决方案。因为清晰可见...错误的网址

http://localhost:8080/SampleWebProject/restservices/productcatalog/search?name=Hammer现在工作正常。

关于休息服务 - Tomcat 能够启动,主页工作正常但不能执行任何 GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56786380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com