解決返回JSON報錯:HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowE

問題描述:

持久層框架用的是JPAhtml

在Controller返回JSON數據到前端的時候 出現了這個StackOverflowError的錯誤
即 在將對象轉換爲JSON格式的數據的時候出現了無限遞歸調用的狀況
前端

以下:java

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: net.zjitc.xxx.pojo.XXX["XXX"]->net.zjitc.xxx.pojo.XXX["XXX"]->org.hibernate.collection.internal.PersistentBag[0]->net.zjitc.xxx.pojo.XXX["XXX"]->net.zjitc.xxx.pojo.XXX["XXX"]->org.hibernate.collection.internal.PersistentBag[0]->net.zjitc.xxx.pojo.XXX["XXX"]->net.zjitc.xxx.pojo.XXX["XXX"]->org.hibernate.collection.internal.PersistentBag[0]->net.zjitc.xxx.pojo.XXX["XXX"]->net.zjitc.xxx.pojo.XXX["XXX"]->org.hibernate.collection.internal.PersistentBag[0]->net.zjitc.xxx.pojo.XXX["XXX"]->net.zjitc.xxx.pojo.XXX["XXX"]->org.hibernate.collection.internal.PersistentBag[0]->net.zjitc.xxx.pojo.XXX["XXX"]->net.zjitc.xxx.pojo.XXX["XXX"]->org.hibernate.collection.internal.PersistentBag[0]->net.zjitc.xxx.pojo.XXX["XXX"])
	at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:296)
	at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:103)
	at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:290)
	at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:180)
	at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

緣由:

實體類之間互相關聯 在序列化A實體類的時候 因爲A裏有B類 而後去序列化B 在序列化B實體類的時候 又因爲B裏有A類 而後去序列化A
如此反覆遞歸 從而形成該問題
web

解決方法:

在有關聯關係的屬性的getter方法(請注意 是getter方法不是setter方法)上添加@JsonBackReference@JsonManagedReference註解spring

  • 的一方加上@JsonBackReference註解
  • 而且在的一方加上@JsonManagedReference註解

這兩個註解一般是配對使用的
@JsonBackReference註解標註的屬性在序列化(serialization)的時候會被忽略 即結果中的json數據不包含該屬性的內容
@JsonManagedReference註解標註的屬性則會被序列化

apache

例一:json

@ManyToOne
private Type type; 



@JsonBackReference
public Type getType() {
    return type;
}

public void setType(Type type) {
    this.type = type;
}

例二:tomcat

@OneToMany(mappedBy = "type")
private List<User> users=new ArrayList<>();


    
@JsonManagedReference
public List<User> getUsers() {
    return users;
}

public void setUsers(List<User> users) {
    this.users = users;
}

發佈了190 篇原創文章 · 獲贊 8 · 訪問量 71萬+
相關文章
相關標籤/搜索