Venkat
Venkat

Reputation: 1384

Jackson JSON not expanding attributes of object

I'm facing a weird problem with JSON serialization. I've JPA objects which I'm using annotation @JsonProperty to serialize & deserialize objects to JSON data.

Query. 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

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

The above method returns a List

But the Serialized JSON object doesn't contain the entire child object details from the 2nd item in the list. The 1st JSON object has everything correctly. Then 2nd JSON object in the list is missing some object details:

Output 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": "[email protected]",
            "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": "[email protected]",
        "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": "[email protected]",
                    "query": "What is the cause of spine disc lapse ?",
                    "queryDate": 1439137989000,
                    "queryAnswered": true,
                    "responses": [
                        {
                            "responseId": 2,
                            "query": 6,
                            "respondedByUser": {
                                "email": "[email protected]",
                                "userName": "User B",
                                "userType": {
                                    "userTypeId": 10002,
                                    "userType": "Expert"
                                },
                                "institutionDetails": 1004,
                                "registeredDate": 1439136400000,
                                "mobileNumber": "12346578"
                            },
                            "response": "Incorrect seating position",
                            "responseDate": 1439138916000
                        }
                    ]
                },
                "respondedByUser": "[email protected]",
                "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. 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 ?

Upvotes: 1

Views: 1410

Answers (1)

StaxMan
StaxMan

Reputation: 116572

That is because you are asking Jackson to use Object Ids, with annotation @JsonIdentityInfo. So after initially serializing objects completely, further references refer to the object id.

Upvotes: 1

Related Questions