Java 如何让@JsonIgnore工作,使JSON不会递归返回?
我有以下Java类Java 如何让@JsonIgnore工作,使JSON不会递归返回?,java,jackson,spring-boot,Java,Jackson,Spring Boot,我有以下Java类 @Component @JsonIgnoreProperties({"begin", "end"}) public class Event extends ResourceSupport { @JsonProperty("name") private final String name; @JsonProperty("description") private final String description; @JsonProp
@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;
这将在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
...
我还尝试将@JsonIgnore
直接放在它们上面
以下是控制器:
@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);
}
@RequestMapping(“/api/hello”)
@应答器
HttpEntity getEvent(){
事件=新事件(“名称”、“描述”和“欧洲/巴黎”分区),
LocalDateTime.now().plusDays(1),LocalDateTime.now().plusDays(2));
add(linkTo(methodOn(EventApi.class).getEvent()).withSelfRel());
返回新的响应状态(事件,HttpStatus.OK);
}
我也在试用SpringHateoas,所以我不确定这是否与它有关
由于SpringBoot的固执己见,我是否应该使用不同的开发模式?对于
JsonIgnoreProperties
要进行序列化,您必须指定要忽略的变量名,例如
@JsonIgnoreProperties({"begin", "end", "timeZone"})
根据文档,这些是逻辑名,例如有名为getBegin()
和getEnd()的getter
通过注释字段声明或其getter,还可以在序列化过程中获取要忽略的字段。
e、 g.1
e、 g.2
由于字段名是在@JsonIgnoreProperties注释中硬编码的,因此重命名字段时有可能出错。因此,@JsonIgnore比@JsonIgnoreProperties更受欢迎
@JsonIgnore
private final LocalDateTime begin;
@JsonIgnore
public LocalDateTime getBegin() {
return begin;
}