【发布时间】:2021-05-28 23:26:26
【问题描述】:
我正在做一个项目,我请求一个 api 来发布项目详细信息。以下是我尝试使用 HttpRequest 插入正文 itemstr 的代码
public void test(){
newItem item = new newItem(12,12,12,21,"waiwai");
JSONObject itemobj = new JSONObject(item);
String itemStr = itemobj.toString();
System.out.println(itemStr);
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://localhost:3001/postMongo")).POST(HttpRequest.BodyPublishers.ofString(itemStr)).build();
}
这是我的 newItem 类
public class newItem {
int id,wholesaleRate,SellingPrice,stock;
String name;
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getWholesaleRate() {
return wholesaleRate;
}
public void setWholesaleRate(int wholesaleRate) {
this.wholesaleRate = wholesaleRate;
}
public int getSellingPrice() {
return SellingPrice;
}
public void setSellingPrice(int sellingPrice) {
SellingPrice = sellingPrice;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public newItem(int id, int wholesaleRate, int sellingPrice, int stock, String name) {
this.id = id;
this.wholesaleRate = wholesaleRate;
this.SellingPrice = sellingPrice;
this.stock = stock;
this.name = name;
}
}
我正在尝试将 itemStr 作为正文发布到我在本地运行的宁静 API。我可以使用 GET 请求检索但无法发布。
【问题讨论】:
-
你必须解释更多。老实说,我不明白你想做什么。向我们提供有关“newItem”和您想要的最终对象格式的更多信息。
-
我已经编辑了我的帖子。如果您能提供帮助,我将非常高兴
标签: java http httprequest