JSON 反对 BODY 发布者
JSON object to BODY publisher
我正在做一个项目,我请求 api 到 post 项目详细信息。以下是我尝试使用 HttpRequest
插入正文项的代码
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();
}
这是我的新商品class
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;
}
}
我正在尝试 post 将 itemStr 作为主体添加到我 运行 本地的 restful API。我可以使用 GET 请求检索但不能 post.
我推荐使用GSON
UserEntity user = new UserEntity();
user.setUserName("UserName");
user.setUserAge(18);
Gson gson = new Gson();
String jsonStr = gson.toJson(user);
请参考此 post 将您的对象转换为 JSON:
Converting Java objects to JSON with Jackson
OpenJDK 发布了桥接新 Java 11 HTTP 客户端和 Jackson 的方法:
http://openjdk.java.net/groups/net/httpclient/recipes.html#jsonPost
ObjectMapper objectMapper = new ObjectMapper();
String requestBody = objectMapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(map);
HttpRequest request = HttpRequest.newBuilder(uri)
.header("Content-Type", "application/json")
.POST(BodyPublishers.ofString(requestBody))
.build();
return HttpClient.newHttpClient()
.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse::statusCode)
.thenAccept(System.out::println);
希望 Jackson 能很快实现 BodyPublisher
本身,而不必先序列化为字符串...
我正在做一个项目,我请求 api 到 post 项目详细信息。以下是我尝试使用 HttpRequest
插入正文项的代码 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();
}
这是我的新商品class
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;
}
}
我正在尝试 post 将 itemStr 作为主体添加到我 运行 本地的 restful API。我可以使用 GET 请求检索但不能 post.
我推荐使用GSON
UserEntity user = new UserEntity();
user.setUserName("UserName");
user.setUserAge(18);
Gson gson = new Gson();
String jsonStr = gson.toJson(user);
请参考此 post 将您的对象转换为 JSON: Converting Java objects to JSON with Jackson
OpenJDK 发布了桥接新 Java 11 HTTP 客户端和 Jackson 的方法: http://openjdk.java.net/groups/net/httpclient/recipes.html#jsonPost
ObjectMapper objectMapper = new ObjectMapper();
String requestBody = objectMapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(map);
HttpRequest request = HttpRequest.newBuilder(uri)
.header("Content-Type", "application/json")
.POST(BodyPublishers.ofString(requestBody))
.build();
return HttpClient.newHttpClient()
.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse::statusCode)
.thenAccept(System.out::println);
希望 Jackson 能很快实现 BodyPublisher
本身,而不必先序列化为字符串...