简体   繁体   English

杰克逊JSON不扩展对象的属性

[英]Jackson JSON not expanding attributes of object

I'm facing a weird problem with JSON serialization. 我面对JSON序列化的怪异问题。 I've JPA objects which I'm using annotation @JsonProperty to serialize & deserialize objects to JSON data. 我有一些JPA对象,正在使用批注@JsonProperty将对象序列化和反序列化为JSON数据。

Query. 查询。 java java的

@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 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 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);
    }

The above method returns a List 上面的方法返回一个List

But the Serialized JSON object doesn't contain the entire child object details from the 2nd item in the list. 但是,序列化JSON对象不包含列表中第二项的全部子对象详细信息。 The 1st JSON object has everything correctly. 第一个JSON对象正确无误。 Then 2nd JSON object in the list is missing some object details: 然后列表中的第二个JSON对象缺少一些对象详细信息:

Output 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
            }
        ]
    }
]

If you notice in the 1st result (queryId: 7), department Object & respondedByUser object will be expanded whereas in the 2nd result (queryId: 6) the objects department & respondedByUser are not expanded. 如果您在第一个结果(queryId:7)中注意到,部门对象和respondedByUser对象将被展开,而在第二个结果(queryId:6)中,部门和respondedByUser对象不会被扩展。 When I debug & see the values before the serialization, the objects are having their respective values properly. 当我调试并查看序列化之前的值时,对象正确地具有各自的值。

Why is it happening so ? 为什么会这样呢? Am I missing something ? 我想念什么吗?

That is because you are asking Jackson to use Object Ids, with annotation @JsonIdentityInfo . 那是因为您@JsonIdentityInfo Jackson使用带有注释@JsonIdentityInfo对象ID。 So after initially serializing objects completely, further references refer to the object id. 因此,在最初完全序列化对象之后,其他参考将引用对象ID。

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

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