服务器在Spring ResponseEntity中返回HttpSession对象时发生StackOverflow错误

时间:2018-06-01 14:26:02

标签: spring spring-mvc recursion jackson stack-overflow

在我的Spring MVC Rest控制器方法中,我试图在另一个名为HttpSession的自定义对象中返回一个Token对象,所有这些对象都在Spring ResponseEntity对象中,返回客户。

但出于某种原因,每当我将Stackoverflow对象包含为Jackson时,由于某些无限递归 HttpSession数据绑定问题,我收到Token错误我的StackOverflow课程中的一个属性。

我的HttpSession课程中Token对象时,我没有收到Token错误。

这是我的public class Token implements Serializable { private static final long serialVersionUID = 1L; private HttpSession session; private String value; public Token () {} public Token (HttpSession session, String value) { this.session = session; this.value = value; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public HttpSession getSession() { return session; } public void setSession(HttpSession session) { this.session = session; } } 课程:

@RequestMapping(value = "/getData", method = RequestMethod.GET)
public ResponseEntity<Token> getData(HttpServletRequest request) {
    HttpSession session = request.getSession();
    Token t = new Token(session, "value");
    return new ResponseEntity<Token>(t, HttpStatus.OK);
}

这是我的控制器方法:

Root cause of ServletException. 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: weblogic.servlet.internal.WebAppServletContext["securityContext"]->weblogic.servlet.internal.ServletSecurityContextImpl["servletContext"]............... com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) at ........-

任何人都知道为什么会这样吗?

这是堆栈跟踪:

BeautifulSoup

0 个答案:

没有答案