英文:
How to create a parent child Json response in Spring Boot with a Enum using Java
问题
我正在尝试从Spring Boot应用程序创建一个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;
TreeEnum(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<TreeEnum> responses) {
this.responses = responses;
}
}
英文:
I am trying to create a json response from a Spring boot application where the name of the enum will be the parent structure and below that all the child will be residing . What i have created is showing only the child hierarchy but i want the parent also .Like i want below
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: "",
},
]
what i am getting is
[
{
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: "",
},
]
Below are codes what i have used
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;
}
}
答案1
得分: 1
你正在返回项目的集合,需要返回一个带有包含这些集合的属性项的对象。
这可以通过使用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());
}
英文:
You are returning collection of the items, you need to return object with attribute items that will contain the collections.
This can be accomplished with Map.
@GetMapping("/getEnumResponse")
public Map getCurrentContent() {
Map response = new HashMap();
response.put("items", TreeEnum.values());
return response;
}
If you are on Java9 or beyond you can do
@GetMapping("/getEnumResponse")
public Map getCurrentContent() {
return Map.of("items", TreeEnum.values());
}
答案2
得分: 0
创建一个ItemDto,在其中传递TreeEnum的列表。如果您不想要列表,并且在您的映射中启用根值,请使用@JsonRootName("Item")。
英文:
Create an ItemDto and in that you pass List of TreeEnum. YOu can also use @JsonRootName("Item") if you don't want list and in your mapping enable root value mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论