Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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 当密钥不同时,将类似类型的数据映射到bean_Java_Spring_Spring Boot - Fatal编程技术网

Java 当密钥不同时,将类似类型的数据映射到bean

Java 当密钥不同时,将类似类型的数据映射到bean,java,spring,spring-boot,Java,Spring,Spring Boot,我有以下json数据,并希望创建可用于映射到它的对象。 能够很好地绘制地图,但不可行。 在本例中,我添加了itemOne、itemTwo和itemThree 我最终不得不为每一个创建一个对象。 在实际场景中,我有100多个这样的类,创建100个类是没有意义的 这些键的值与对象数组中的值相同。 有没有一种方法可以重用单个对象来映射这些对象 因为我需要通过REST调用来匹配这些名称,以便在Spring中捕获这些数据 例如: ResponseEntity<Foo> response = r

我有以下json数据,并希望创建可用于映射到它的对象。
能够很好地绘制地图,但不可行。
在本例中,我添加了itemOne、itemTwo和itemThree

我最终不得不为每一个创建一个对象。
在实际场景中,我有100多个这样的类,创建100个类是没有意义的

这些键的值与对象数组中的值相同。
有没有一种方法可以重用单个对象来映射这些对象

因为我需要通过REST调用来匹配这些名称,以便在Spring中捕获这些数据

例如:

ResponseEntity<Foo> response = restTemplate
  .exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class);
当前创建的对象将通过ObjectMapper映射这些对象

@Getter
@Setter
class Foo {
    MainBody mainBody;
}

@Getter
@Setter
class ItemOne {
    String header;
}

@Getter
@Setter
class ItemThree {
    String header;
}

@Getter
@Setter
class ItemTwo {
    String header;
}

@Getter
@Setter
class MainBody {
    SubData subData;
}

import java.util.List;

@Getter
@Setter
class SubData {
    List<ItemOne> itemOne;
    List<ItemTwo> itemTwo;
    List<ItemThree> itemThree;
}
@Getter
@塞特
福班{
主体主体;
}
@吸气剂
@塞特
第一类{
字符串头;
}
@吸气剂
@塞特
第三类{
字符串头;
}
@吸气剂
@塞特
第二类{
字符串头;
}
@吸气剂
@塞特
类主体{
子数据子数据;
}
导入java.util.List;
@吸气剂
@塞特
类子数据{
列出第一项;
列出项目二;
列出项目三;
}

如果我理解正确,那么您可以使用以下内容:

@Getter
@塞特
类主体{
地图子数据;
}
@吸气剂
@塞特
类项目{
字符串头;
}
其中
子数据
中的映射键将是
“itemOne”
“itemTwo”

@Getter
@Setter
class Foo {
    MainBody mainBody;
}

@Getter
@Setter
class ItemOne {
    String header;
}

@Getter
@Setter
class ItemThree {
    String header;
}

@Getter
@Setter
class ItemTwo {
    String header;
}

@Getter
@Setter
class MainBody {
    SubData subData;
}

import java.util.List;

@Getter
@Setter
class SubData {
    List<ItemOne> itemOne;
    List<ItemTwo> itemTwo;
    List<ItemThree> itemThree;
}