将 Yaml 属性对象转换为 Java 对象

Convert Yaml properties object to Java object

这是我的 Yaml 属性文件

bb:
  employees:
   -
    employee1:
      name: Syed
      locations:
        - HYD
        - MAA
   -
    employee2:
      name: Adhil
      locations:
        - BOM
        - DEL

我想在我的应用程序中将对象转换为我的 POJO。但是我做不到,它总是 returns null。

有什么我遗漏的吗?

@Data

@Builder
 @AllArgsConstructor
@NoArgsConstructor
@Configuration
@ConfigurationProperties(prefix = "bb")
public class EmpConfig {

    EmployeeDetails employees;
}

员工详细信息 Class

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Configuration

public class EmployeeDetails {

    List<Map<String, List<String>>> details;
}

我是这样工作的:

YAML(称为 application.yaml)(删除了 employeeIds (employee1, employee2) 之前的破折号)

bb:
  employees:
    employee1:
      name: Syed
      locations:
        - HYD
        - MAA
    employee2:
      name: Adhil
      locations:
        - BOM
        - DEL

配置class:

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Configuration
@ConfigurationProperties(prefix = "bb")
public class EmpConfig {
    private Map<String, EmployeeDetails> employees;
}

员工详细信息:

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class EmployeeDetails {
    private String name;
    private List<String> locations;
}

所以基本上你的语法和数据类型有问题,所以Spring无法解析配置文件。

这对我有用,无需修改您的结构。

YAML 看起来像:

bb:
  employees:
      employee:
        - name: Syed
          locations:
            - HYD
            - MAA

        - name: Adhil
          locations:
            - BOM
            - DEL

输出为

希望对您有所帮助!