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

Tags: , ,



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

Answer

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


Source: stackoverflow