Map using Jackson with inheritance

Asked

Viewed 75 times

0

I have the Queuecontent class that has is a superclass of two others.

I receive a JSON format string that contains the information I need to extract. The super class is:

@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() {
    }
}

(I’m using Lombok to generate Getters/Setters)

This is the daughter 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() {
    }
}

My mapping is done as follows:

               EmailCameraOffline infosEmail = new ObjectMapper().readValue(content, EmailCameraOffline.class);
                System.out.println(infosEmail);

And the output is:

EmailCameraOffline(camera_last_online=2020-03-12 03:01:45.0, camera_nome=Pier Cam 1, empresa_url_plataforma=null)

How do I get my Emailcameraoffline object to have the attributes of the superclass initialized?

  • Object mapper has a property called enabledefaulttyping(), in theory it allows this filling through inheritance, test there please

  • Did not serve :( E is marked as deprecated;

  • what version of Jackson are you using? I copied your classes here and tested against version 2.2.3 and it worked

  • I’m using the 2.10.3!

  • edits the question with the json you’re sending out nicely

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.