Spring Boot项目中读取yaml配置文件的解决方法
                            来源:爱站网时间:2022-11-11编辑:网友分享
                        
                        
                        今天所带来的Spring Boot项目中读取yaml配置文件的解决方法希望小伙伴们都能看一看,本文详细的讲解了文章内容,如果有需要,爱站技术频道小编整理的资料一定对你有帮助。
                        在我的Spring Boot项目中,我有一个POJO类,用于读取yaml配置文件。
@Configuration
@ConfigurationProperties("filenet")
@Data
public class ApplicationConfig {
    @NotNull(message = "Input directory cannot be null")
    @NotBlank(message = "Input directory cannot be blank")
    private String **inputDir**;
    @NotNull(message = "Working directory cannot be null")
    @NotBlank(message = "Working directory cannot be blank")
    private String **workingDir**;
    @Pattern(regexp = "[0-9]+",message = "Invalid value for SMTP port")
    private String port;
}
有时发生inputDir或workingDir或配置文件的其他字段为空的情况。我正在使用javax.validation.constraints检查空白或空值。在这种情况下,以及在启动应用程序时,我会看到一条异常消息,程序终止。例如:Port具有必须为数字的验证。我想做的是妥善处理此异常,并向相关团队发送电子邮件以解决此异常。
我创建了一个用于验证配置文件内容的类
@Component
public class ConfigParamValidation {
    public List<String>  validateApplicationConfigFile(ApplicationConfig applicationConfig) {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<ApplicationConfig.ContentEngine>> contentEngineVoilations = validator.validate(applicationConfig.contentEngine);
        exceptionMessgae = contentEngineVoilations.stream().map(ConstraintViolation::getMessage).collect(Collectors.toList());
        if(!exceptionMessgae.isEmpty()) {
            through new ConfigFileException(<by passing all required params>);
        }
    }
}
我试图创建一个扩展RuntimeException的类
public class ConfigFileException extends RuntimeException {
    private String message;
    private List<String> details;
    private String hint;
    private String nextActions;
    private String support;
    private HttpStatus httpStatus;
    private ZonedDateTime zonedDateTime;
    public ConfigFileException(String message) {
        super(message);
    }
    public ConfigFileException(String message, Throwable cause) {
        super(message, cause);
    }
    public ConfigFileException(String message, Throwable cause, List<String> details, String hint, String nextActions, String support, HttpStatus httpStatus, ZonedDateTime zonedDateTime) {
        super(message, cause);
        this.message = message;
        this.details=details;
        this.hint=hint;
        this.nextActions=nextActions;
        this.support=support;
        this.httpStatus=httpStatus;
        this.zonedDateTime = zonedDateTime;
    }
}
带有@ExceptionHandler的另一个类
@Data
@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class SupportTeamException {
    @ExceptionHandler(value = {ConfigFileException.class})
    public ResponseEntity<Object> handleConfigFileException(ConfigFileException e) {
        ConfigFileException configFileException = new ConfigFileException(e.getMessage(), e, e.getDetails(), e.getHint(), e.getNextActions(), e.getSupport(), e.getHttpStatus(), e.getZonedDateTime());
        return new ResponseEntity<Object>(configFileException,e.getHttpStatus());
    }
}
问题是由于某种原因,控件未传递到我可以发送电子邮件的SupportTeamException类。
如果你对Spring Boot项目中读取yaml配置文件的解决方法比较满意的话,可以收藏下本网站,我们每天都会更新不同的技术文章,需要的朋友就请来翻阅查看吧!
 
                    