【问题标题】:Jackson JSON not expanding attributes of object杰克逊 JSON 没有扩展对象的属性
【发布时间】:2015-08-11 18:15:56
【问题描述】:

我在 JSON 序列化方面遇到了一个奇怪的问题。我有 JPA 对象,我使用注释 @JsonProperty 将对象序列化和反序列化为 JSON 数据。

查询。爪哇

@Entity
@XmlRootElement
public class Query {

    @Id
    @GeneratedValue
    private int queryId;

    @ManyToOne
    @JoinColumn(name="institution_id")
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property="institutionId")
    private InstitutionDetails institution;

    @ManyToOne
    @JoinColumn(name="department_id")
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property="deptId")
    private Department department;

    @ManyToOne
    @JoinColumn(name="topic_id")
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property="topicId")
    private Topic topic;

    @ManyToOne
    @JoinColumn(name="raised_by_user_id")
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property="email")
    private User raisedByUser;

    @Lob 
    private String query;

    @Column(name="query_date")
    private Date queryDate;

    @Column(name="query_answered")
    private boolean queryAnswered;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="query", fetch=FetchType.LAZY)
    private Set<Response> responses;

    @JsonProperty
    public int getQueryId() {
        return queryId;
    }

    public void setQueryId(int queryId) {
        this.queryId = queryId;
    }

    @JsonProperty
    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    @JsonProperty
    public Topic getTopic() {
        return topic;
    }

    public void setTopic(Topic topic) {
        this.topic = topic;
    }

    @JsonProperty
    public User getRaisedByUser() {
        return raisedByUser;
    }

    public void setRaisedByUser(User raisedByUser) {
        this.raisedByUser = raisedByUser;
    }

    @JsonProperty
    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    @JsonProperty
    public Date getQueryDate() {
        return queryDate;
    }

    public void setQueryDate(Date queryDate) {
        this.queryDate = queryDate;
    }

    @JsonProperty
    public boolean isQueryAnswered() {
        return queryAnswered;
    }

    public void setQueryAnswered(boolean queryAnswered) {
        this.queryAnswered = queryAnswered;
    }

    @JsonProperty
    public Set<Response> getResponses() {
        return responses;
    }

    public void setResponses(Set<Response> responses) {
        this.responses = responses;
    }

    @JsonProperty
    public InstitutionDetails getInstitution() {
        return institution;
    }

    public void setInstitution(InstitutionDetails institution) {
        this.institution = institution;
    }
}

Department.java

@Entity
@XmlRootElement
public class Department {

    @Id
    @GeneratedValue
    private int deptId;

    @Column(name="department_name", length=100)
    private String departmentName;

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    @JoinColumn(name="department_id")
    private Set<Topic> topic;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="department", fetch=FetchType.LAZY)
    private Set<Query> queries;

    @JsonProperty
    public int getDeptId() {
        return deptId;
    }

    public void setDeptId(int deptId) {
        this.deptId = deptId;
    }

    @JsonProperty
    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    @JsonProperty
    public Set<Topic> getTopic() {
        return topic;
    }

    public void setTopic(Set<Topic> topic) {
        this.topic = topic;
    }

    @JsonIgnore
    public Set<Query> getQueries() {
        return queries;
    }

    public void setQueries(Set<Query> queries) {
        this.queries = queries;
    }
}

QueryController.java

@GET
    @Path("/getAllAnsweredQueries/{institutionId}/{departmentId}/{topicId}")
    @Produces({MediaType.APPLICATION_JSON})
    public List<Query> getAllAnsweredQueries(@PathParam("institutionId") int institutionId, @PathParam("departmentId") int departmentId, @PathParam("topicId") int topicId) {
        return m_queryService.getAllAnsweredQueries(institutionId, departmentId, topicId);
    }

上述方法返回一个List

但序列化 JSON 对象不包含列表中第二项的整个子对象详细信息。第一个 JSON 对象具有正确的一切。然后列表中的第二个 JSON 对象缺少一些对象详细信息:

输出 JSON

[
    {
        "queryId": 7,
        "institution": {
            "institutionId": 1004,
            "instituionName": "A College"
        },
        "department": {
            "deptId": 1,
            "departmentName": "Anatomy",
            "topic": [
                {
                    "topicId": 1003,
                    "topicName": "Nervous System"
                },
                {
                    "topicId": 1002,
                    "topicName": "Muscular System"
                },
                {
                    "topicId": 1006,
                    "topicName": "Vascular System"
                },
                {
                    "topicId": 1005,
                    "topicName": "Cells & Tissues"
                },
                {
                    "topicId": 1004,
                    "topicName": "Circulatory Sytem"
                },
                {
                    "topicId": 1001,
                    "topicName": "Skeletal System"
                }
            ]
        },
        "topic": {
            "topicId": 1001,
            "topicName": "Skeletal System"
        },
        "raisedByUser": {
            "email": "abcd@gmail.com",
            "userName": "User A",
            "userType": {
                "userTypeId": 10001,
                "userType": "Student"
            },
            "institutionDetails": 1004,
            "registeredDate": 1439136336000,
            "mobileNumber": "12346578"
        },
        "query": "What causes bone damage ?",
        "queryDate": 1439139172000,
        "queryAnswered": false,
        "responses": []
    },
    {
        "queryId": 6,
        "institution": 1004,
        "department": 1,
        "topic": {
            "topicId": 1002,
            "topicName": "Muscular System"
        },
        "raisedByUser": "abcd@gmail.com",
        "query": "What is the cause of spine disc lapse ?",
        "queryDate": 1439137989000,
        "queryAnswered": true,
        "responses": [
            {
                "responseId": 2,
                "query": {
                    "queryId": 6,
                    "institution": 1004,
                    "department": 1,
                    "topic": 1002,
                    "raisedByUser": "abcd@gmail.com",
                    "query": "What is the cause of spine disc lapse ?",
                    "queryDate": 1439137989000,
                    "queryAnswered": true,
                    "responses": [
                        {
                            "responseId": 2,
                            "query": 6,
                            "respondedByUser": {
                                "email": "mnop@gmail.com",
                                "userName": "User B",
                                "userType": {
                                    "userTypeId": 10002,
                                    "userType": "Expert"
                                },
                                "institutionDetails": 1004,
                                "registeredDate": 1439136400000,
                                "mobileNumber": "12346578"
                            },
                            "response": "Incorrect seating position",
                            "responseDate": 1439138916000
                        }
                    ]
                },
                "respondedByUser": "mnop@gmail.com",
                "response": "Incorrect seating position",
                "responseDate": 1439138916000
            }
        ]
    }
]

如果您在第一个结果 (queryId: 7) 中注意到,department 对象和 respondByUser 对象将被展开,而在第二个结果 (queryId: 6) 中,对象 department 和 respondByUser 没有展开。当我在序列化之前调试并查看值时,对象正确地具有各自的值。

为什么会这样?我错过了什么吗?

【问题讨论】:

    标签: java json jpa jackson


    【解决方案1】:

    那是因为您要求 Jackson 使用带有注释 @JsonIdentityInfo 的对象 ID。所以在最初完全序列化对象之后,进一步的引用引用了对象 id。

    【讨论】:

    • 我没听懂你?如果我需要完整的对象,我该怎么办?
    • 删除 @JsonIdentityInfo 注释。但回想一下早期阶段并尝试回想最初添加注释的原因可能是件好事——可能有添加它们的原因?
    • 我添加它们是为了避免序列化双向关系时出现堆栈溢出错误。无论如何,感谢您的提示。我将它们保留在父实体上并在子级实体中删除它们,这样杰克逊就不会重复生成导致堆栈溢出错误。
    • 好的,有道理。我只是猜测它可能是从其他用例中剪切的。很高兴您的问题得到解决。
    猜你喜欢
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 2015-10-12
    • 2021-12-08
    • 2017-08-04
    • 1970-01-01
    相关资源
    最近更新 更多