Java Jackson JSON未扩展对象的属性
我在JSON序列化方面遇到了一个奇怪的问题。我有一些JPA对象,我正在使用annotation@JsonProperty将对象序列化和反序列化为JSON数据 查询。javaJava Jackson JSON未扩展对象的属性,java,json,jpa,jackson,Java,Json,Jpa,Jackson,我在JSON序列化方面遇到了一个奇怪的问题。我有一些JPA对象,我正在使用annotation@JsonProperty将对象序列化和反序列化为JSON数据 查询。java @Entity @XmlRootElement public class Query { @Id @GeneratedValue private int queryId; @ManyToOne @JoinColumn(name="institution_id") @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;
}
}
@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;
}
}
@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);
}
如果注意到在第一个结果(queryId:7)中,department对象和ResponsedByUser对象将展开,而在第二个结果(queryId:6)中,department对象和ResponsedByUser对象不会展开。当我调试并查看序列化之前的值时,对象正确地拥有各自的值
为什么会这样?我错过什么了吗 这是因为您要求Jackson使用对象ID和注释
@JsonIdentityInfo
。因此,在最初完全序列化对象之后,进一步的引用将引用对象id。我没有理解您的意思吗?如果我需要完整的对象,那么我应该怎么做?删除@JsonIdentityInfo
注释。但是,回想一下前面的阶段,试着回想一下最初添加注释的原因可能是好的——添加注释可能是有原因的?我添加注释是为了避免序列化双向关系时出现堆栈溢出错误。无论如何,谢谢你的提示。我将它们保留在父实体上,并在子实体上删除它们,这样jackson就不会在重复生成中导致堆栈溢出错误。好的,这是有意义的。我只是猜测它可能是从其他用例中剪下来的。很高兴你的问题解决了。
[
{
"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
}
]
}
]