从外部yaml文件解析对象列表

问题描述 投票:0回答:1

我有一个外部 YAML 文件,其中仅包含对象列表。每次调用 loadVerificatedEmailsConfiguration 方法时,我都需要读取此文件。我正在尝试使用 Apache Commons Configuration2 库来解决这个问题。不幸的是,我只能从文件中加载单个属性,例如verified-emails.email,但不能加载整个单个对象。我需要这个,因为有些属性是可选的。

YAML 文件:

verificated-emails:
  - { email: [email protected], firstname: Dummy, surname: xxxx }
  - { email: [email protected], firstname: Bob, surname: xxx }
  - { email: [email protected], surname: Dusan }
  - { email: [email protected], firstname: tomas, surname: xxx }

loadVerificatedEmails配置方法

 public List<VerificatedEmailsConfiguration> loadVerificatedEmailsConfiguration() throws ConfigurationException, IOException, org.apache.commons.configuration2.ex.ConfigurationException {
            log.info("start loadVerificatedEmailsConfiguration");
            YAMLConfiguration yamlConfiguration = new YAMLConfiguration();
            if(VERIFICATED_EMAIL_CONFIG == null) {
                throw new ConfigurationException("FilePath is null");
            }
            List<VerificatedEmailsConfiguration> verificatedEmailsConfigurationList = new ArrayList<>();
            InputStream inputStream = new FileInputStream(VERIFICATED_EMAIL_CONFIG);
            yamlConfiguration.read(inputStream);
            var verifyEmails = yamlConfiguration.getProperty("verificated-emails");
            log.info("verifyEmails: {}", verifyEmails);
    
            return verificatedEmailsConfigurationList;
        }

VerifiedEmails配置模型

@Data
public class VerificatedEmailsConfiguration {
    private String email;
    private String firstname;
    private String surname;
}

日志

2024-05-10T13:23:59.145+02:00  INFO 17340 --- [mail] [           main] c.e.m.c.ExternalConfiguration            : start loadVerificatedEmailsConfiguration
2024-05-10T13:23:59.192+02:00  INFO 17340 --- [mail] [           main] c.e.m.c.ExternalConfiguration            : verifyEmails: null
java spring-boot yaml apache-commons
1个回答
0
投票

您可以使用 Jackson 轻松实现这一点

需要依赖

  <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
            <version>2.12.5</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.12.5</version>
        </dependency>

你的Pojo

public class MainYaml {
@JsonProperty("verificated-emails")
    private List<VerificatedEmail> verificatedEmailList;

    public List<VerificatedEmail> getVerificatedEmailList() {
        return verificatedEmailList;
    }

    public void setVerificatedEmailList(List<VerificatedEmail> verificatedEmailList) {
        this.verificatedEmailList = verificatedEmailList;
    }
}

public class VerificatedEmail {

    private String email;
    private String firstname;
    private String surname;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }
}

转换代码

ObjectMapper mapper = new YAMLMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MainYaml mainYaml = new MainYaml();
File file = new File("D:/test/test.yaml");
mainYaml =  mapper.readValue(file,MainYaml.class);
© www.soinside.com 2019 - 2024. All rights reserved.