【发布时间】: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