有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java序列化字段中具有包装器名称的对象列表

我必须序列化第三方类的实例,这些序列化的对象将被另一个工具使用
所以我不能更改类,它们在第三方库中

在我的应用程序中,我使用Jackson(com.fasterxml.Jackson)和ObjectMapper(com.fasterxml.Jackson.databind.ObjectMapper)进行序列化
我们班有一份员工名单 我有一个Employee(List)列表,并且使用的工具希望Employee标识符包装Employee对象,就像它是一个映射一样。见下文

第三方类别:

class Employee{
 String id;
 String location;
 String department;
} 

class Division{
 List<Employee> employees;
}

目前我正在使用Mixin接口来隐藏一些我不需要的其他字段

interface EmployeeMixin {
    @JsonProperty String getId();
    @JsonProperty("location") String getLocation();
    @JsonProperty("department") String getDepartment();
}

并将mixin添加到对象映射器中,如下所示:`

ObjectMapper myYamlMapper=Jackson.newObjectMapper(new YAMLFactory());
myYamlMapper.addMixIn(Employee.class, EmployeeMixin.class);

我使用对象映射器生成的结果是:

employees:
- id: "sales0051"
  location: "london"
  department: "sales"

而我应该产生的输出如下:

employees:
  sales0051:
    location: "london"
    department: "sales"

我怎样才能产生我需要的输出?它看起来像一张地图,也许我应该朝那个方向走,但不知道怎么走


共 (0) 个答案