【问题标题】:Spring: Can't map YAML to list of objectsSpring:无法将 YAML 映射到对象列表
【发布时间】:2019-12-24 19:33:32
【问题描述】:

我有一个包含错误和错误消息的 yaml 文件。我试图将它们加载到一个类中,RefreshErrors 在我的 Spring Boot 微服务使用的一个公共 jar 中。常见的 jar 在类路径上确实有 Spring Boot,但没有 SpringBootApplication - 它只是被其他应用程序包含。所以这可能就是我在RefreshErrors 中没有得到任何值的原因?

这是 yaml,位于 application.yml 中,位于 src/main/resources 下:

refreshErrorsProperties:
  refreshErrors:
    -
      recordStatus: "Error"
      errorCode: "502 Bad Gateway"
      errorMessage: "AWS service is tempoarily not available. ESS has been notified. Please try refreshing again after the service becomes available."
    -
      recordStatus: "Error"
      errorCode: "503 Service Temporarily Unavailable"
      errorMessage: "AWS service is tempoarily not available. ESS has been notified. Please try refreshing again after the service becomes available."
    -
...

这是我希望 yaml 映射到的类的配置:

@Data
@Component
@ConfigurationProperties(prefix="refreshErrors")
public class RefreshErrorsProperties {
    private List<RefreshErrors> refreshErrors = new ArrayList<>();
}

RefreshErrors 类:

@Data
@AllArgsConstructor
public class RefreshErrors {
    private String errorCode;
    private String recordStatus;
    private String errorMessage;
}

我在另一个类中自动连接RefreshErrors(见下文),我确实得到了一个对象引用,但里面的所有值(如errorCode 等)都是空的。任何帮助将不胜感激!

@Autowired
public void setRefreshErrorsProperties(RefreshErrorsProperties refreshErrorsProperties) {
    RefreshJobDetailHelper.refreshErrorsProperties = refreshErrorsProperties;
}
...
    RefreshErrors error;
    if (exception != null) {
        String fullException = ExceptionUtils.getStackTrace(exception);
        error = refreshErrorsProperties.getRefreshErrors().stream()
                .filter(f -> fullException.contains(f.getErrorCode()))
                .findAny()
                .orElseGet((Supplier<? extends RefreshErrors>) new RefreshErrors("Error", ERROR.getValue(), "An error has occurred while refreshing this record. ESS has been notified. Please try refreshing again after the issue has been resolved."));
    } else {
       error =  new RefreshErrors("No data", ERROR_NO_DATA.getValue(), "Data is not available in the source for this product, domain, and effective date.");
    }

【问题讨论】:

  • 您可以尝试在@EnableConfigurationProperties(RefreshErrors.class) 中注册您的课程,看看它是否有效?并从组件更改为@Configuration

标签: spring spring-boot yaml


【解决方案1】:

这里的问题是前缀@ConfigurationProperties(prefix = "refreshErrors")。应该像@ConfigurationProperties,没有前缀,需要映射前缀下的属性时需要前缀,但refreshErrors是你要映射的属性而不是前缀。下面的代码工作正常。 我已经重构了一点:

属性文件:

    refresh-errors:
      -
        record-status: "Error"
        error-code: "502 Bad Gateway"
        error-message: "AWS service is tempoarily not available. ESS has been notified. Please try refreshing again after the service becomes available."
      -
        record-status: "Error"
        error-code: "503 Service Temporarily Unavailable"
        error-message: "AWS service is tempoarily not available. ESS has been notified. Please try refreshing again after the service becomes available."

PropertiesMapperClass:

@Data
@Component
@ConfigurationProperties
public class RefreshErrorsProperties {
    private List<RefreshErrors> refreshErrors = new ArrayList<>();

    @Data
    public static class RefreshErrors {
        private String errorCode;
        private String recordStatus;
        private String errorMessage;
    }
}

【讨论】:

  • @Monit Singh,我刚试了你的课,我收到一条错误消息,提示“必须指定前缀”。我做错了什么?
  • 奇怪的是,虽然 Intellij 报告该错误,但该类仍然编译。
  • 我需要能够实例化RefreshErrors
  • 这很奇怪,因为它对我来说很好,即使从它说的文档中,默认前缀值也是空白的。你确定没有错字,因为它说无效的类字段?否则,您可以尝试定义前缀,例如 errors.refresh-errors: @ConfigurationProperties(prefix="errors")
  • 谢谢 Mohit,我完全按照你的建议做了,并添加了另一个前缀。我还在苦苦挣扎,我会更新我的问题以显示我现在所拥有的。
【解决方案2】:

如果你不改变你的注解,yaml文件节点refreshErrors的属性不应该是一个列表, yaml 文件应该是

refresh-errors:
  record-status: "Error"
  error-code: "502 Bad Gateway"
  error-message: "AWS service ..."

这可能不是你想要的, 如果您不更改 yaml 文件, 代码应该像 Mohit Singh 的节目

【讨论】:

    猜你喜欢
    • 2016-07-13
    • 2017-01-17
    • 2015-12-12
    • 2019-07-25
    • 2017-02-17
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    相关资源
    最近更新 更多