搜尋
首頁JavaDTO 序列化期間 Spring Boot 應用程式中出現 StackOverflowError 問題

問題內容

當我嘗試序列化具有自引用關係的 dto 類別 (nodeattributesdto) 時,我在 spring boot 應用程式中遇到 stackoverflowerror。該錯誤發生在執行dto類別中的tostring方法期間。

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 請求內文:

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

錯誤:

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

問題:

  1. 如何修改 nodeattributesdto 類別中的 tostring 方法以避免序列化期間出現 stackoverflowerror?
  2. 在處理 dto 中的自引用關係時,我是否應該考慮特定的 jackson 註解或配置?

環境: 春季啟動版本:2.7.2 java版本:17 資料庫:postgresql

#我已經嘗試過:

  • 不同的 jackson 註釋(@jsonmanagedreference、@jsonbackreference)

正確答案


我相信您的困惑是 toString 不控制 Spring Boot 中的編組。

如果您要透過 System.err.println() 記錄該物件以表示標準錯誤,它將使用該 toString。

似乎您的 toString 本質上是試圖成為資料的遞歸轉儲,但並不正確。我認為這只是基本的 Java/CS。

在toString中,您可以只列印目前節點的數據,然後對所有子節點呼叫toString(delagate)。應該可以做到這一點。我認為一般情況下您不需要反向引用(對於 toString),因為您將從“樹”的頂部開始。

編組器檢查物件並使用反射來組成序列化表示。正如您所注意到的,它將遵守某些註釋。例如@JsonIgnore。

請參閱:如何忽略 json 中的欄位回應?

這裡有很多好資訊:https://www.php.cn/link/ffe4a40fecc90fa1120088e704712fb2

它還可能有助於在程式碼生成工具(如 jhipster)之外創建一個簡單的 Web 服務,以了解幕後發生的情況,從而更好地控制生成。

以上是DTO 序列化期間 Spring Boot 應用程式中出現 StackOverflowError 問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:stackoverflow。如有侵權,請聯絡admin@php.cn刪除

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器