【问题标题】:Json request body to java object with spring-boot使用spring-boot向java对象请求Json请求体
【发布时间】:2019-05-19 09:07:03
【问题描述】:

我有以下 JSON 请求

{
  "FreightCalculationRequest": {
    "Products": [
      {
        "sku": "123",
        "size": "S",
        "quantity": "1",
        "shipAlone": "True",
        "itemType": "Shoe"
      },
      {
        "sku": "123",
        "size": "S",
        "quantity": "1",
        "shipAlone": "True",
        "itemType": "Shoe"
      }
    ],
    "ShipToZip": "54452",
    "IsCommercial": "True"
  }
}

我正在尝试将此请求作为自定义 java 对象发送到 API 控制器方法,然后将相同的对象作为 json 格式的字符串返回。但是,我通过邮递员得到回复,对于产品,shiptoZip 我得到一个空值,对于 isCommercial 我得到假,但我什至没有 false 作为请求中 isCommercial 的值。这是怎么回事?我不知道如何在 Java 中很好地调试,因为我基本上每次都通过输入 mvn spring-boot:start 来检查我的应用程序

这是我返回的对象,并将其用作控制器方法的参数。

public class FreightCalculationRequest {

    private Product[] Products;
    private String ShipToZip;
    private boolean IsCommercial;

    public Product[] getProducts() { return this.Products; }
    public void setProducts(Product[] itemsRequest) { this.Products = itemsRequest; }

    public String getShipToZip() { return this.ShipToZip; }
    public void setShipToZip(String ShipToZip) { this.ShipToZip = ShipToZip; }

    public boolean getIsCommercial() { return this.IsCommercial; }
    public void setIsCommercial(boolean IsCommercial) { this.IsCommercial = IsCommercial; }

}

这是我正在调用的控制器方法

@RequestMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE,  method = RequestMethod.POST)
FreightCalculationRequest TestCall(@RequestBody FreightCalculationRequest calculationRequest) {
    return calculationRequest;

}

为什么我的响应与进来的请求不一样。

更新: 我在变量中添加了@JsonProperty,现在响应看起来像这样

{
    "isCommercial": false,
    "shipToZip": null,
    "products": null,
    "Products": null,
    "ShipToZip": null,
    "IsCommercial": false
}

有点失落,也意识到我可以在 mvn 运行时保存我的更改,它会自动编译更改

更新: 因此,当我最初删除 json 响应中“FreightCalculationRequest”的包装时,我的 json 中的 itemType 实际上抛出了一个错误,所以我认为这是问题所在,但是 itemType 实际上是代码中的一个对象,所以这是由于我没有放入有效属性并彻底读取 json 解析错误,我有两种解决方案,将响应包装在另一个类中,或者删除 FreightCalculationWrapping。

我还了解到我需要添加@JsonProperty 来映射json

非常感谢

【问题讨论】:

  • 您的 JSON 请求看起来像另一个模型类包装了 FreightCalculationRequest 属性。打开它,它应该可以工作。
  • @TheHeadRush 我试过了,现在我没有得到响应,而是得到一个 json 解析错误。
  • 这是不遵循命名约定的情况。如果您遵循适当的约定,这将正常工作。例如shipToZip 不是 ShipToZip。无论如何,这很奇怪,因为您在有效负载中混合了命名约定。
  • 我看到我得到的错误实际上是一个不同的问题。

标签: java spring


【解决方案1】:

但是,我通过邮递员收到了关于产品的回复,并且 shiptoZip 我得到一个空值,对于 isCommercial 我得到一个错误,但我没有 甚至在请求中将 false 作为 isCommercial 的值。什么是 继续?

您必须将 FreightCalculationRequest 包装在一个新的模型类中。

创建一个新的包装器类,

public class FreightCalculationRequestWrapper {
    @JsonProperty("FreightCalculationRequest")
    private FreightCalculationRequest freightCalculationRequest;

    ...
}

使用这个新的 Wrapper 类来处理您的请求:

@RequestMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE,  method = RequestMethod.POST)
FreightCalculationResponse TestCall(@RequestBody FreightCalculationRequestWrapper calculationRequest) {
    return calculationRequest;

}

另外,JSON 中的属性名称以大写字母开头。

如果您使用的是 Jackson,那么您可以在模型字段上使用 @JsonProperty(...) 注释来正确映射它们。

例如:

public class FreightCalculationRequest {
    @JsonProperty("Products")
    private Product[] Products;

    .
    .
    .
}

【讨论】:

  • 我是否需要对 Request 对象中的对象执行此操作
  • @boo 您需要删除它,因为您的 JSON 在根目录下有 FreightCalculationRequest 而不是 Products
  • 好的,我开始了,我向 Products 添加了注释,现在我得到了 { "isCommercial": false, "shipToZip": null, "products": null, "Products": null } 的响应,当我将注释添加到所有变量时,它会在 json 响应中保留小写名称.
  • 所以除了最后一个问题外,我的一切工作正常,现在产品显示 json 中的内容作为响应,但它也将其复制为“产品”对象,所以我有两个products 数组在我的响应中,试图调整它,但我有我的映射集。
  • @boo 查看@JsonSetter@JsonGetter 注释。你会希望它们出现在你的 Getter-Setter 上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-05
  • 2021-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
相关资源
最近更新 更多