获取json数据时如何避免无限循环

时间:2017-04-11 12:41:28

标签: json spring spring-boot spring-restcontroller

我有两个实体Projet和FinancementExt 这是Projet:

@Entity public class Projet implements Serializable{
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id_projet;
@Column(name="ref",unique=true)
private String ref;
private String description;
@OneToMany(mappedBy="projet",fetch=FetchType.LAZY)
private Collection <FinancementExt> fe;
public void setFe(Collection <FinancementExt> fe)
{
    this.fe = fe;
}
public Collection<FinancementExt> getFe()
{
    return fe;
}

FinancementExt实体:

    @Entity public class FinancementExt implements Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private long id_fe;
private String ref_fe;
private double montant;
private double taux_change;
private String monnaie;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="id_prj")
private Projet projet;
public long getid_prj()
{
    return projet.getId_projet();
}
public void setId_projet(long id_prj)
{
    projet.setId_projet(id_prj);
}
public Projet getProjet() {
    return projet;
}
public void setProjet(Projet projet) {
    this.projet = projet;
}

当我调用restService listfe时,它会启动一个无限循环 这是listfe的restservice代码:

@RequestMapping(value="/listfe",method=RequestMethod.GET)
public Page<FinancementExt> listFE(int page, int size) {
    return FeMetier.listFE(page, size);
}

FeMetier:

public interface FeMetier {
public FinancementExt addFE(FinancementExt fe);
public void deleteFE(FinancementExt fe);
public FinancementExt editFE(FinancementExt fe);
public Page<FinancementExt> listFE(int page,int size);
public Page<FinancementExt> listFEByPrj(long id_prj,int page, int size);

}

FeMetierImp是服务:

@Service public class FeMetierImp implements FeMetier{
@Autowired
FinancementExtRepository FeRepository;
@Override
public FinancementExt addFE(FinancementExt fe) {
    return FeRepository.save(fe);   
}
@Override
public void deleteFE(FinancementExt fe) {
    FeRepository.delete(fe);
}
@Override
public FinancementExt editFE(FinancementExt fe) {
    return FeRepository.save(fe);
}
@Override
public Page<FinancementExt> listFE(int page,int size) {
    return FeRepository.findAll(new PageRequest(page, size));
}
@Override
public Page<FinancementExt> listFEByPrj(long id_prj, int page, int size) {
    return FeRepository.findAllByIdprojet(id_prj, new PageRequest(page, size));
}

}

这是我得到的错误:

at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) ~[jackson-databind-2.8.6.jar:2.8.6]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690) ~[jackson-databind-2.8.6.jar:2.8.6]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.6.jar:2.8.6]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) ~[jackson-databind-2.8.6.jar:2.8.6]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690) ~[jackson-databind-2.8.6.jar:2.8.6]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.6.jar:2.8.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149) ~[jackson-databind-2.8.6.jar:2.8.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112) ~[jackson-databind-2.8.6.jar:2.8.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.8.6.jar:2.8.6]

这是我在使用@JsonIgnore时遇到的错误:

2017-04-11 15:04:16.645  WARN 10852 --- [nio-8088-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.data.domain.PageImpl["content"]->java.util.Collections$UnmodifiableRandomAccessList[0]->com.projet.model.FinancementExt["projet"]->com.projet.model.Projet_$$_jvstbac_93["handler"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.data.domain.PageImpl["content"]->java.util.Collections$UnmodifiableRandomAccessList[0]->com.projet.model.FinancementExt["projet"]->com.projet.model.Projet_$$_jvstbac_93["handler"])

0 个答案:

没有答案