【问题标题】:How to override @JsonInclude from controller in Spring boot如何在 Spring Boot 中从控制器覆盖 @JsonInclude
【发布时间】:2020-04-09 16:51:01
【问题描述】:

我有一个 POJO 类,该类具有注释 @JsonInclude(Include.NON_NULL)

@JsonInclude(Include.NON_NULL)
@Getter
@Setter
public class Attribute implements Serializable {
    private String type;
    private List<Object> value;
    private String unit;
    private List<Object> metaInfo;
}

控制器看起来像这样。

    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
  public Attribute getSingle() {
      return getAttribute(); // getAttribute() will get the object of type Attribute.
  }

现在,我有一个需求,根据 type 字段的值,需要在最终生成的 JSON 中为 value 字段返回 null。

例如,如果 type='XYZ',则响应将为 { type:'XYZ'}。如果 type='ABC',那么响应应该是{ type:'ABC',value:null}

如何在 Spring Boot 中实现这一点?处理这种情况的最佳方法是什么?

【问题讨论】:

标签: java spring spring-boot jackson objectmapper


【解决方案1】:

这有帮助吗?

if (value.getClass() == Integer) {
      return null;
}

【讨论】:

    猜你喜欢
    • 2020-06-21
    • 1970-01-01
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 2015-12-27
    • 2016-06-06
    相关资源
    最近更新 更多