简体   繁体   English

杰克逊忽略了父母的所有领域

[英]Jackson is ignoring all fields from parent

I have 2 classes Parent and a Child . 我有2个班级Parent和一个Child

Parent

public abstract class Parent implements Serializable{
    protected String id;
    protected String deletedAt;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getDeletedAt() {
        return deletedAt;
    }
    public void setDeletedAt(String deletedAt) {
        this.deletedAt = deletedAt;
    }
}

Child 儿童

public class Child extends Parent{
    private String name;
    public String getName(){
    ...
}

when i load the Child and try to go like this : 当我加载孩子并试图像这样:

Child c = getChildFromDb();
c.getId();

and this is my jackson config: 这是我的杰克逊配置:

JacksonConfig JacksonConfig

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

@Configuration
public class JacksonConfig {

    @Bean
    @Primary
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        objectMapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
        return objectMapper;
    }
}

the ID is null but the Name is there. ID为null但名称在那里。 What i am doing wrong ? 我做错了什么?

It turns out to be a problem with the DB Driver of 'rethink' for some reason rethink DB driver ignored the fields of the parent class and this caused that those fields were never stored in the DB so they were never retrieved. 由于某种原因,重新思考数据库驱动程序忽略了父类的字段,这导致这些字段从未存储在数据库中,因此从未检索过它们,因此“重新思考”的数据库驱动程序出现问题。 Changed the DB with its driver to MongoDB and it works perfectly now. 将DB及其驱动程序更改为MongoDB,现在可以正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM