Home > Java > body text

StackOverflowError issue in Spring Boot application during DTO serialization

王林
Release: 2024-02-05 23:06:04
forward
624 people have browsed it
Question content

I am getting a stackoverflowerror in my spring boot application when I try to serialize a dto class (nodeattributesdto) with a self-referential relationship. The error occurs during execution of tostring method in dto class.

nodeattributes.java:

// relevant parts of nodeattributes.java
@onetomany(mappedby = "parent")
@cache(usage = cacheconcurrencystrategy.read_write)
@jsonignoreproperties(value = { "children", "parent", "node" }, allowsetters = true)
private set<nodeattributes> children ;

@manytoone
@jsonignoreproperties(value = { "children", "parent", "node" }, allowsetters = true)
private nodeattributes parent;

// other fields, getters, setters, etc.
Copy after login

nodeattributesdto.java:

// relevant parts of nodeattributesdto.java
private set<nodeattributesdto> children;
private nodeattributesdto parent;

// getters, setters, and other methods...

@override
public string tostring() {
    return "nodeattributesdto{" +
        "id=" + getid() +
        // other fields...
        ", parent=" + getparent() +
        ", children=" + getchildren() +
        ", node=" + getnode() +
        "}";
}
Copy after login

postmapping Request text:

{
  // some other fields...
  "children": [
    {
      "key": "attribute412w",
      "value": "value3",
      "valuetype": "integer",
      "type": "response",
      "required": false,
      "enabled": true,
      "node": {
        "id": 26030
      }
    }
  ],
  // other fields...
}
Copy after login

mistake:

{
    "type": "https://www.jhipster.tech/problem/problem-with-message",
    "title": "Internal Server Error",
    "status": 500,
    "detail": "Handler dispatch failed; nested exception is java.lang.StackOverflowError",
    "path": "/api/node-attributes",
    "message": "error.http.500"
}
Copy after login

question:

  1. How to modify the tostring method in the nodeattributesdto class to avoid stackoverflowerror during serialization?
  2. Are there any specific jackson annotations or configurations I should consider when dealing with self-referential relationships in dtos?

environment: Spring boot version: 2.7.2 java version: 17 Database: postgresql

I've tried:

  • Different jackson annotations (@jsonmanagedreference, @jsonbackreference)

Correct answer


I believe your confusion is that toString does not control marshalling in Spring Boot.

If you want to log this object to standard error via System.err.println(), it will use the toString.

It seems like your toString is essentially trying to be a recursive dump of data, but that's not correct. I think it's just basic Java/CS.

In toString, you can just print the data of the current node, and then call toString(delagate) on all child nodes. It should be possible to do this. I think in general you don't need backreferences (for toString) since you'll be starting from the top of the "tree".

The marshaller examines the object and uses reflection to compose a serialized representation. As you noticed, it will respect certain annotations. For example @JsonIgnore.

See: How to ignore field responses in json?

There is a lot of good information here: https://www.php.cn/link/ffe4a40fecc90fa1120088e704712fb2

It may also be useful to create a simple web service outside of a code generation tool (such as jhipster) to understand what is going on behind the scenes and thus have better control over the generation.

The above is the detailed content of StackOverflowError issue in Spring Boot application during DTO serialization. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template