使用具有继承性的 Jackson 将字符串映射到 Object
Map String to Object using Jackson with inheritance
我的 QueueContent class 是另外两个的超级class。
我得到一个 JSON 格式的字符串,其中包含我需要提取的信息。超级class是:
@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)
这是child class:
@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 object 初始化 superclass 属性?
一切都应该加载和初始化就好了,所以调用:
System.out.println(infosEmail.getEmpresa());
应该给出预期值。
问题
问题出在 toString()
方法(通过 @Data
完成)的默认实现 EmailCameraOffline
class,它不包括继承的字段。
解决方案
要解决此问题,您可以 "override" @Data
的 toString()
实施来包括继承字段以及使用 Lombok 作为:
@Data
@ToString(callSuper = true)
public class EmailCameraOffline extends QueueContent {
...
}
我的 QueueContent class 是另外两个的超级class。
我得到一个 JSON 格式的字符串,其中包含我需要提取的信息。超级class是:
@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)
这是child class:
@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 object 初始化 superclass 属性?
一切都应该加载和初始化就好了,所以调用:
System.out.println(infosEmail.getEmpresa());
应该给出预期值。
问题
问题出在 toString()
方法(通过 @Data
完成)的默认实现 EmailCameraOffline
class,它不包括继承的字段。
解决方案
要解决此问题,您可以 "override" @Data
的 toString()
实施来包括继承字段以及使用 Lombok 作为:
@Data
@ToString(callSuper = true)
public class EmailCameraOffline extends QueueContent {
...
}