无法从java spring api获取ressource,可能是jpa故障

无法从java spring api获取ressource,可能是jpa故障,java,angular,spring,api,jpa,Java,Angular,Spring,Api,Jpa,我的应用程序是一个Angular web应用程序,通过hibernate和jpa从JavaAPI获取资源。我将按照执行顺序粘贴相关代码,然后粘贴错误(即使错误不明确) 角度服务呼叫后端: const API_URL = 'http://localhost:8080/communication/'; public getBugReports(): Observable<any> { return this.http.get(API_URL + 'bugreport'); }

我的应用程序是一个Angular web应用程序,通过hibernate和jpa从JavaAPI获取资源。我将按照执行顺序粘贴相关代码,然后粘贴错误(即使错误不明确)

角度服务呼叫后端:

const API_URL = 'http://localhost:8080/communication/';

public getBugReports(): Observable<any> {
    return this.http.get(API_URL + 'bugreport');
}
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:472) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:129) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
.
.
.
[Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.arnaudg.springjwt.models.User["suggestions"]

(the rest is not very relevant)
实体用户:

@Entity
@Table( name = "users", 
        uniqueConstraints = { 
            @UniqueConstraint(columnNames = "username"),
            @UniqueConstraint(columnNames = "email") 
        })
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int idUser;

    @NotBlank
    @Size(max = 20)
    private String username;

    @NotBlank
    @Size(max = 50)
    @Email
    private String email;

    @NotBlank
    @Size(max = 120)
    private String password;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable( name = "user_roles", 
                joinColumns = @JoinColumn(name = "user_id"), 
                inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Set<Role> roles = new HashSet<>();

    @Column(name = "avatar", length = 1000)
    private byte[] avatar;

    @OneToMany(targetEntity = Suggestion.class, mappedBy="user")
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    private List<Suggestion> suggestions = new ArrayList<>();

... Constructors, getters/setters etc...

实体建议类型:

@Entity
@Table(name = "suggestion_types")
public class SuggestionType {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int idSuggestionType;

    @Enumerated(EnumType.STRING)
    @Column(length = 30)
    @Cascade(CascadeType.ALL)
    private ESuggestionType name;

... Constructors, getters/setters etc...
正面错误:

HttpErrorResponse
error: {error: SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at XMLHttpRequest.onL…, text: "[{"idBugReport":1,"user":{"idUser":1,"username":"E…ggestions\"])","path":"/communication/bugreport"}"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure during parsing for http://localhost:8080/communication/bugreport"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://localhost:8080/communication/bugreport"
__proto__: HttpResponseBase
注意最后的StackOverflower错误,我认为我的jpa映射是错误的,特别是在多个、一个多个、可连接的注释上。我是一名前端开发人员,所以我几乎是java jpa方面的专家


如果您发现我的代码中有任何错误或改进,请告诉我,我非常乐意知道。如果你想看另一节课或者别的,就问我。谢谢大家

当控制器将响应发送回浏览器时,发生的JSON转换过程不理解
用户
建议
对象之间的父子关系。用户对象包含建议(列表)对象,建议实体包含用户对象。因此,在生成JSON时,转换运行到一个无限循环中


@JsonManagedReference
注释注释
用户
实体中的
建议
属性,用
@JsonBackReference
注释
建议
实体中的
用户
属性。这将告诉JSON转换器关系的类型以及停止的位置。另请参见此处:

add Suggestion.class definition。你说得对,我的天啊,非常感谢你,我已经做了3/4天了,效果非常好
@Entity
@Table(name = "suggestion_types")
public class SuggestionType {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int idSuggestionType;

    @Enumerated(EnumType.STRING)
    @Column(length = 30)
    @Cascade(CascadeType.ALL)
    private ESuggestionType name;

... Constructors, getters/setters etc...
HttpErrorResponse
error: {error: SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at XMLHttpRequest.onL…, text: "[{"idBugReport":1,"user":{"idUser":1,"username":"E…ggestions\"])","path":"/communication/bugreport"}"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure during parsing for http://localhost:8080/communication/bugreport"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://localhost:8080/communication/bugreport"
__proto__: HttpResponseBase
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:472) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:129) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
.
.
.
[Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.arnaudg.springjwt.models.User["suggestions"]

(the rest is not very relevant)