【问题标题】:JSON object to BODY publisherJSON 对象到 BODY 发布者
【发布时间】: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


【解决方案1】:

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 本身,而不必先序列化为字符串...

【讨论】:

    【解决方案2】:

    我推荐使用GSON

    UserEntity user = new UserEntity();
    user.setUserName("UserName");
    user.setUserAge(18);
    
    Gson gson = new Gson();
    String jsonStr = gson.toJson(user);
    

    请参阅这篇文章将您的对象转换为 JSON: Converting Java objects to JSON with Jackson

    【讨论】:

    • 上面打印的 itemStr 是 {"salesPrice":12,"wholesaleRate":12,"name":"waiwai","id":12,"stock":21} 。如果我使用 .POST(HttpRequest.BodyPublishers.ofString(itemStr)),我认为 BODY 必须是字符串
    • 这不是你想要的吗?
    • 它没有向 api 发送任何内容。但是当我在邮递员中发送上面的输出时,它是正确的
    猜你喜欢
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多