Home >Java >StackOverflowError issue in Spring Boot application during DTO serialization

StackOverflowError issue in Spring Boot application during DTO serialization

王林
王林forward
2024-02-05 23:06:04654browse
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.

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() +
        "}";
}

postmapping Request text:

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

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"
}

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete