1.禁止使用lombok 的@Data 注释

  使用@Data注释后,默认会重写父类的toString()方法,hashcode()等方法,在往map里存的时候,会根据equals和hashcode方法,来计算下标,而如果@Data注释的类与其他类有关联的属性(如:@onetoone,@onetomany等)且关联的属性不为空时,会不断从关联方的属性进行查找,再从关联方查找该@Data注释的类,依次循环,造成堆栈溢出。

  例子:考试信息与报名信息以 1:n  进行关联

  

package com.apply.entity;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerator;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import javax.persistence.*;
import java.util.Date; /**
* @author tianp
* 报名信息 apply_info
*/
@Data
@Entity
@Table(name = "apply_info")
@ApiModel(value = "ApplyInfo", description = "报名信息对象")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class ApplyInfo {
@Id
@GeneratedValue
@ApiModelProperty(value = "id", dataType = "long")
private Long id; @ManyToOne
@JoinColumn(name = "examNum")
private ExamInfo examInfo; }

  

package com.apply.entity;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.Data; import javax.persistence.*;
import java.util.Date;
import java.util.Set; /**
* @author tianp
* 考试信息设置 apply_examinfo
*/
@Data
@Entity
@Table(name = "apply_examinfo")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class ExamInfo {
@Id
@GeneratedValue
private Long id; @OneToMany(mappedBy = "examInfo", cascade = CascadeType.ALL)
private Set<ApplyInfo> applyInfos; }
public class Test1 {
@Test
public void testObject() {
ExamInfo examInfo = new ExamInfo();
ApplyInfo applyInfo = new ApplyInfo();
Set<ApplyInfo> set = new HashSet<>();
set.add(applyInfo);
examInfo.setApplyInfos(set);
applyInfo.setExamInfo(examInfo);
System.out.println(examInfo.toString());
System.out.println(applyInfo.toString());
}
}
java.lang.StackOverflowError
at java.lang.StringBuilder.append(StringBuilder.java:136)
at com.apply.entity.ExamInfo.toString(ExamInfo.java:16)
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at com.apply.entity.ApplyInfo.toString(ApplyInfo.java:17)
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at com.apply.entity.ExamInfo.toString(ExamInfo.java:16)
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at com.apply.entity.ApplyInfo.toString(ApplyInfo.java:17)
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at com.apply.entity.ExamInfo.toString(ExamInfo.java:16)
。。。。。。

  

2.关联属性使一方的属性在json转换时生效

使用SpringMVC 默认的jackson 进行json转换时,存在关联属性,使用@JsonIgnore忽略属性(在one的一方进行忽略,)

05-11 21:53