如何在Spring Boot中使用Java创建一个带有枚举的父子Json响应

huangapple go评论69阅读模式
英文:

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: &quot;ABC &quot;,
subTitle: &quot;&quot;,
description:
&quot;123  &quot;,
url: &quot;&quot;,
},
{
title: &quot;Revenue&quot;,
subTitle: &quot;&quot;,
description:
&quot;Digitally &quot;,
url: &quot;&quot;,
},
{
title: &quot;xyz&quot;,
subTitle: &quot;&quot;,
description:
&quot;345,&quot;,
url: &quot;stackoverflow.com&quot;,
},
{
title: &quot;kji&quot;,
subTitle: &quot;&quot;,
description:
&quot;890&quot;,
url: &quot;&quot;,
},
{
title: &quot;KOI&quot;,
subTitle: &quot;&quot;,
description:
&quot;054,&quot;,
url: &quot;&quot;,
},
]

what i am getting is

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

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(&quot;/getEnumResponse&quot;)
public List&lt;TreeEnum&gt; 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 (&quot;Pay&quot;,&quot;&quot;,&quot;Digitally.&quot;,&quot;&quot;),
VALUE2 (&quot;Revenue&quot;,&quot;&quot;,&quot;enable&quot;,&quot;&quot;),
VALUE3(&quot;Banking&quot;,&quot;&quot;,&quot;Treasury Reporting etc&quot;,&quot;CHOICE&quot;),
VALUE4(&quot;Accounting&quot;,&quot;&quot;,&quot;Corporate&quot;,&quot;&quot;);
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&lt;TreeEnum &gt; responses;
public List&lt;TreeEnum &gt; getResponses() {
return responses;
}
public void setResponses(List&lt;Response&gt; 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(&quot;/getEnumResponse&quot;)
public Map getCurrentContent() {
   Map response = new HashMap();
   response.put(&quot;items&quot;, TreeEnum.values());
   return response;
} 

If you are on Java9 or beyond you can do

@GetMapping(&quot;/getEnumResponse&quot;)
public Map getCurrentContent() {
   return Map.of(&quot;items&quot;, 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);

huangapple
  • 本文由 发表于 2020年8月5日 19:14:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63264000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定