我遇到了JSON序列化的奇怪问题。我使用注释@JsonProperty的JPA对象来序列化&将对象反序列化为JSON数据。
查询。的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);
}
上述方法返回List
但是Serialized 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
}
]
}
]
如果您注意到第1个结果(queryId:7),则部门对象&amp; respondByUser对象将被扩展,而在第二个结果(queryId:6)中,对象部门&amp; respondByUser没有扩展。当我调试&amp;查看序列化之前的值,对象正确地具有各自的值。
为什么会这样?我错过了什么吗?
答案 0 :(得分:1)
那是因为你要求Jackson使用带有注释@JsonIdentityInfo
的Object ID。因此,在最初完全序列化对象之后,进一步的引用引用了对象id。