简体   繁体   English

Spring Boot Rest 服务 Angular

[英]Spring boot Rest service Angular

I have an application in front end i use angular and back end i use Spring boot.我在前端有一个应用程序,我使用 angular,后端我使用 Spring Boot。 In my front end i must upload a CSV file that insert data in tables.在我的前端,我必须上传一个在表格中插入数据的 CSV 文件。 So i send data to the backend which save it.所以我将数据发送到保存它的后端。 My problem: i have a class Individus with relation @OneToMany to others class like as comptes.我的问题:我有一个类 Individus 与 @OneToMany 与其他类的关系,如 comptes。 So when i try to get All individus with this Rest service : http://localhost:8080/api/individus , i have a parsing json data error.因此,当我尝试使用此 Rest 服务获取 All individus 时: http://localhost:8080/api/individus 时,出现解析 json 数据错误。

at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) ~[jackson-databind-2.9.6.jar:2.9.6]

Here is my class Individus:这是我的班级 Individus:

@Entity 

public class Individu implements Serializable {公共类 Individu 实现了 Serializable {

@Id
private String nui;
private int civility; 
private String lastName;
private String useName;
private String firstName;
@Temporal(TemporalType.DATE)
@JsonFormat(pattern="dd/MM/yyyy")
private Date birthDate;
private String birthPlace;
private String birthCountry;
private String birthCountryLib;
private String nationality;
@OneToMany(mappedBy="individu", fetch = FetchType.LAZY)
private Collection<Compte> comptes;
@OneToMany(mappedBy="individu", fetch = FetchType.LAZY)
private Collection<Adresse> adresses;
@OneToMany(mappedBy="individu", fetch = FetchType.LAZY)
private Collection<Contact> contacts;
@OneToMany(mappedBy="individu", fetch = FetchType.LAZY)
private Collection<Iban> ibans;ode here

Is some one have a solution for me?有人对我有解决方案吗?

You didn't provide enough of the stacktrace to show what the actual error is, but I suspect that you're running into a problem with a circular graph, as the objects in your Individu class' collections (eg Compte, Ardresse) probably hold a reference to the parent Individu instance.您没有提供足够的堆栈跟踪来显示实际错误是什么,但我怀疑您遇到了圆形图的问题,因为您的 Individu 类集合(例如 Compte、Ardresse)中的对象可能包含对父 Individu 实例的引用。

The solution in that case is to add a @JsonIgnore annotation to the children's references to the parent.在这种情况下,解决方案是在子级对父级的引用中添加 @JsonIgnore 注释。 This is most likely an attribute in the child that is currently marked with an @ManyToOne annotation.这很可能是当前使用@ManyToOne 注释标记的子级中的一个属性。

this is another solution这是另一种解决方案

public class Produit{.....
@ManyToOne(optional=false,fetch=FetchType.LAZY)
@JsonBackReference
private Category category;....}


public class Category{....
@OneToMany(fetch=FetchType.LAZY,mappedBy="category")
@JsonManagedReference
private Collection<Produit> produits;....}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM