【问题标题】:jackson JsonInclude Non_Null not ignoring working on nested Object Properties杰克逊 JsonInclude Non_Null 不忽略嵌套对象属性的工作
【发布时间】:2021-04-12 17:00:43
【问题描述】:
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Flight implements Serializable {

    private TrackInformation trackInformation;

    private MiscData miscData;

    private CargoItems cargoItems;

}



Controller class

  

 @RequestMapping(value = "/getFlightByKey", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public Flight getFlightByKey(@RequestBody @Valid final FlightInfoRequestDTO request)throws NoDataFoundException  {
    return flightInfoAppService.getFlightByKey(request.getKey());
}

公共类 TrackInformation 扩展 AbstractRecord {

private static final long serialVersionUID = -5226745652646434627L;

private String sourceFacility;

private ComputerID computerID;

private String sourceTimeStamp;

private Double latitude;

private Double longitude;

private ReportedAltitude reportedAltitude;

private Integer speed;

}

公共抽象类 AbstractRecord 实现 Serializable {

}

现在返回的飞行对象具有嵌套对象的空字段。如何忽略嵌套对象中的空字段。

在 TrackInformation 中覆盖了 hashCode 和 equals。

【问题讨论】:

  • 你能显示嵌套类TrackInformation 吗?

标签: json spring jackson


【解决方案1】:

您还应该在每个嵌套类上添加@JsonInclude(JsonInclude.Include.NON_NULL) 以忽略嵌套类的空值属性

@JsonInclude(JsonInclude.Include.NON_NULL)
public class TrackInformation extends AbstractRecord {

 // properties

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多