如何让@JsonIgnore工作,以便递归返回JSON?

时间:2015-08-30 16:26:05

标签: java jackson spring-boot

我有以下Java类。

0,30 * * * * *

这将在REST服务中返回。无论我做什么,它总是返回@Component @JsonIgnoreProperties({"begin", "end"}) public class Event extends ResourceSupport { @JsonProperty("name") private final String name; @JsonProperty("description") private final String description; @JsonProperty("timeZone") private final ZoneId timeZone; private final LocalDateTime begin; private final LocalDateTime end; 的深层对象表示,如下所示。

LocalDateTime

我还尝试将 ... {"hour":1,"minute":0,"second":0,"nano":0},"midnightEndOfDay":false},{"month":"OCTOBER","timeDefinition":"UTC","standardOffset":{"totalSeconds":3600,"id":"+01:00","rules":{"fixedOffset":true,"transitions":[],"transitionRules":[]}},"offsetBefore":{"totalSeconds":7200,"id":"+02:00","rules":{"fixedOffset":true,"transitions":[],"transitionRules":[]}},"offsetAfter":{"totalSeconds":3600,"id":"+01:00 ... 直接放在他们身上。

以下是控制器:

@JsonIgnore

我也在尝试Spring HATEOAS,所以我不确定这是否与它有关。

由于SpringBoot的固有特性,我应该使用不同的开发模式吗?

1 个答案:

答案 0 :(得分:3)

要使JsonIgnoreProperties能够进行序列化,您必须指定要忽略的变量名称。

@JsonIgnoreProperties({"begin", "end", "timeZone"})

根据文档,这些是逻辑名称,例如有名为getBegin()getEnd()

的getter

通过注释字段声明或其getter,您还可以在序列化期间获取要忽略的字段。 e.g.1

@JsonIgnore
private final LocalDateTime begin;

e.g.2

@JsonIgnore
public LocalDateTime getBegin() {
    return begin;
}

由于字段名称在@JsonIgnoreProperties注释中是硬编码的,因此在重命名字段时可能会出错。因此,@ JsonIgnore优于@JsonIgnoreProperties。

相关问题