我有QueueContent类,它是另外两个的超类。
我得到一个JSON格式的字符串,其中包含我需要提取的信息。超类是:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class QueueContent {
private String empresa;
private String empresa_cor;
private String empresa_contato;
private String empresa_url;
private String empresa_telefone;
private String empresa_idioma;
public QueueContent(String empresa, String empresa_cor, String empresa_contato, String empresa_url, String empresa_telefone, String empresa_idioma) {
this.empresa = empresa;
this.empresa_cor = empresa_cor;
this.empresa_contato = empresa_contato;
this.empresa_url = empresa_url;
this.empresa_telefone = empresa_telefone;
this.empresa_idioma = empresa_idioma;
}
public QueueContent() {
}
}
我正在使用Lombok生成Getters / Setters]
这是子类:
@Data
public class EmailCameraOffline extends QueueContent {
private Timestamp camera_last_online;
private String camera_nome;
private String empresa_url_plataforma;
public EmailCameraOffline(String empresa, String empresa_cor, String empresa_contato, String empresa_url, String empresa_telefone, String empresa_idioma, Timestamp camera_last_online, String camera_nome, String empresa_url_plataforma) {
super(empresa, empresa_cor, empresa_contato, empresa_url, empresa_telefone, empresa_idioma);
this.camera_last_online = camera_last_online;
this.camera_nome = camera_nome;
this.empresa_url_plataforma = empresa_url_plataforma;
}
public EmailCameraOffline() {
}
}
我已经完成:
EmailCameraOffline infosEmail = new ObjectMapper().readValue(content, EmailCameraOffline.class); System.out.println(infosEmail);
输出为:
EmailCameraOffline (camera_last_online = 2020-03-12 03: 01: 45.0, camera_nome = Pier Cam 1, empresa_url_platform = null)
如何获取我的EmailCameraOffline对象以初始化超类属性?
一切都应该加载,所以调用:
System.out.println(infosEmail.getEmpresa());
应提供期望值。
问题出在toString()
类的@Data
方法(通过EmailCameraOffline
完成)的默认实现中,它不包括继承的字段。
要解决此问题,您可以“覆盖” @Data
的toString()
实现,以使用Lombok包括继承的字段:
@Data
@ToString(callSuper = true)
public class EmailCameraOffline extends QueueContent {
...
}