春季:无法将YAML映射到对象列表

时间:2019-12-24 19:33:32

标签: spring spring-boot yaml

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

以下是application.yml中位于src/main/resources下的Yaml:

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.");
    }

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

如果您不更改注释,则yaml文件节点refreshErrors的属性不应为列表, yaml文件应为

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

可能不是您想要的, 如果您不更改yaml文件, 代码应该像Mohit Singh所示