簡體   English   中英

如何使用 Java 在 Spring 引導中創建父子 Json 響應

[英]How to create a parent child Json response in Spring Boot with a Enum using Java

我正在嘗試從 Spring 引導應用程序創建一個 json 響應,其中枚舉的名稱將是父結構,並且所有子結構都將居住在該名稱之下。 我創建的只是顯示子層次結構,但我也想要父級。就像我想要的下面

items: [
  {
    title: "ABC ",
    subTitle: "",
    description:
         "123  ",
    url: "",
  },
  {
    title: "Revenue",
    subTitle: "",
    description:
      "Digitally ",
    url: "",
  },
  {
    title: "xyz",
    subTitle: "",
    description:
      "345,",
    url: "stackoverflow.com",
  },
  {
    title: "kji",
    subTitle: "",
    description:
      "890",
    url: "",
  },
  {
    title: "KOI",
    subTitle: "",
    description:
      "054,",
    url: "",
  },
]

我得到的是

[
  {
    title: "ABC ",
    subTitle: "",
    description:
         "123  ",
    url: "",
  },
  {
    title: "Revenue",
    subTitle: "",
    description:
      "Digitally ",
    url: "",
  },
  {
    title: "xyz",
    subTitle: "",
    description:
      "345,",
    url: "stackoverflow.com",
  },
  {
    title: "kji",
    subTitle: "",
    description:
      "890",
    url: "",
  },
  {
    title: "KOI",
    subTitle: "",
    description:
      "054,",
    url: "",
  },
]

以下是我使用過的代碼

import java.util.Arrays;
import java.util.List;

 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RestController;

 @RestController
 public class EnumController {

@GetMapping("/getEnumResponse")
public List<TreeEnum> getCurrentContent() {
    MyData  mData = new MyData ();
   return Arrays.asList(TreeEnum.values());
    
    } 

   }



import com.fasterxml.jackson.annotation.JsonFormat;

@JsonFormat(shape=JsonFormat.Shape.OBJECT)
public enum TreeEnum {
VALUE1 ("Pay","","Digitally.",""),
VALUE2 ("Revenue","","enable",""),
VALUE3("Banking","","Treasury Reporting etc","CHOICE"),
VALUE4("Accounting","","Corporate","");

private String title;
private String subTitle;
private  String url;
private String description;

Response(String title,String subTitle,String description,String url) {
    this.title = title;
    this.subTitle = subTitle;
    this.description = description;
    this.url = url;
    
}

public String getTitle() {
    return title;
}


public String getSubTitle() {
    return subTitle;
}

public String getUrl() {
    return url;
}

public String getDescription() {
    return description;
    }
  

   }







import java.util.List;

 public class MyData {
 private List<TreeEnum > responses;

  public List<TreeEnum > getResponses() {
    return responses;
  }

    public void setResponses(List<Response> responses) {
    this.responses = responses;
     }
  }

您正在返回項目的集合,您需要返回 object 以及包含 collections 的屬性項目。

這可以通過 Map 來實現。

@GetMapping("/getEnumResponse")
public Map getCurrentContent() {
   Map response = new HashMap();
   response.put("items", TreeEnum.values());
   return response;
} 

如果您使用的是 Java9 或更高版本,您可以這樣做

@GetMapping("/getEnumResponse")
public Map getCurrentContent() {
   return Map.of("items", TreeEnum.values());
} 

創建一個 ItemDto 並在其中傳遞 TreeEnum 列表。 你也可以使用 @JsonRootName("Item") 如果你不想列出並在你的映射中啟用根值 mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM