java 如何让@JsonIgnore 工作,以便不递归返回 JSON?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32298722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get @JsonIgnore to work so that JSON are not returned recursively?
提问by Pompey Magnus
I have the following Java class.
我有以下 Java 类。
@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;
this gets returned in a REST service. Regardless of what I do it always returns this deep object representation of LocalDateTime
, like below.
这在 REST 服务中返回。不管我做什么,它总是返回 的深层对象表示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
...
I have also tried to put @JsonIgnore
directly on them.
我也试过@JsonIgnore
直接放在它们上面。
Below is the controller:
下面是控制器:
@RequestMapping("/api/hello")
@ResponseBody
HttpEntity<Event> getEvent() {
Event event = new Event("name", "description", ZoneId.of("Europe/Paris"),
LocalDateTime.now().plusDays(1), LocalDateTime.now().plusDays(2));
event.add(linkTo(methodOn(EventApi.class).getEvent()).withSelfRel());
return new ResponseEntity<Event>(event, HttpStatus.OK);
}
I am also trying out Spring HATEOAS, so I'm not sure if that has something to do with it.
我也在尝试 Spring HATEOAS,所以我不确定这是否与它有关。
Is there a different development pattern I should be using, because of the opinionated nature of SpringBoot?
由于 SpringBoot 的自以为是,我应该使用不同的开发模式吗?
回答by Manos Nikolaidis
For JsonIgnoreProperties
to work for serialization you must specify the variable name(s) to ignore e.g.
要JsonIgnoreProperties
进行序列化,您必须指定要忽略的变量名称,例如
@JsonIgnoreProperties({"begin", "end", "timeZone"})
According to documentation these are logicalnames e.g. there are getters named getBegin()
and getEnd()
根据文档,这些是逻辑名称,例如有命名的吸气剂getBegin()
和getEnd()
You can also get a field to be ignored during serialization by annotating the field declaration or its getter. e.g.1
您还可以通过注释字段声明或其 getter 来获取在序列化期间被忽略的字段。例如1
@JsonIgnore
private final LocalDateTime begin;
e.g.2
eg2
@JsonIgnore
public LocalDateTime getBegin() {
return begin;
}
Since the field names are hard-coded in @JsonIgnoreProperties annotation, there are chances of making mistakes while renaming the fields. For that reason, @JsonIgnore is preferred over @JsonIgnoreProperties.
由于字段名称是在 @JsonIgnoreProperties 注释中硬编码的,因此在重命名字段时可能会出错。出于这个原因,@JsonIgnore 优于 @JsonIgnoreProperties。