Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java在Spring Boot中使用枚举创建父子Json响应_Java_Rest_Enums - Fatal编程技术网

如何使用Java在Spring Boot中使用枚举创建父子Json响应

如何使用Java在Spring Boot中使用枚举创建父子Json响应,java,rest,enums,Java,Rest,Enums,我试图从一个Spring启动应用程序中创建一个json响应,其中枚举的名称将是父结构,低于父结构的所有子结构都将驻留。我所创建的只是显示子层次结构,但我也需要父层次结构 items: [ { title: "ABC ", subTitle: "", description: "123 ", url: "", }, { title: "R

我试图从一个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;
     }
  }
导入java.util.array;
导入java.util.List;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.RestController;
@RestController
公共类枚举控制器{
@GetMapping(“/getEnumResponse”)
公共列表getCurrentContent(){
MyData mData=新的MyData();
返回Arrays.asList(TreeEnum.values());
} 
}
导入com.fasterxml.jackson.annotation.JsonFormat;
@JsonFormat(shape=JsonFormat.shape.OBJECT)
公共枚举树{
价值1(“支付”、“数字化”、“数字化”),
值2(“收入”、“启用”、“启用”),
价值3(“银行”、“财务报告等”、“选择”),
价值4(“会计”、“公司”、“公司”);
私有字符串标题;
私有字符串字幕;
私有字符串url;
私有字符串描述;
响应(字符串标题、字符串字幕、字符串描述、字符串url){
this.title=标题;
this.subTitle=副标题;
this.description=描述;
this.url=url;
}
公共字符串getTitle(){
返回标题;
}
公共字符串getSubTitle(){
返回字幕;
}
公共字符串getUrl(){
返回url;
}
公共字符串getDescription(){
返回说明;
}
}
导入java.util.List;
公共类MyData{
私人名单答复;
公共列表getResponses(){
回应;
}
公共响应(列出响应){
这一点。反应=反应;
}
}

如果要返回项的集合,则需要返回包含集合的属性项的对象

这可以通过Map实现

@GetMapping(“/getEnumResponse”)
公共地图getCurrentContent(){
Map response=newhashmap();
response.put(“items”,TreeEnum.values());
返回响应;
} 
如果您使用Java9或更高版本,您可以

@GetMapping(“/getEnumResponse”)
公共地图getCurrentContent(){
返回Map.of(“items”,TreeEnum.values());
} 

创建一个ItemDto,并在其中传递TreeEnum列表。如果您不想要列表,也可以使用@JsonRootName(“项”),并在映射中启用根值映射器.enable(SerializationFeature.WRAP\u root\u value)

我需要在哪里定义spring boot应用程序中的“mapper.enable(SerializationFeature.WRAP_ROOT_VALUE)”,以便在对象映射器之后在整个应用程序中启用它。ObjectMapper mapper=新的ObjectMapper();启用(SerializationFeature.WRAP\u ROOT\u值);