Spring boot 通过@ConfigurationProperties绑定空列表
我正在使用Spring boot 通过@ConfigurationProperties绑定空列表,spring-boot,yaml,Spring Boot,Yaml,我正在使用@ConfigurationProperties将配置属性从YAML文件绑定到java属性类 在properties类中有以下字段: private List<Map<String, Map<String, String>>> actionsBeforeIndexing; 这很有效。但我也希望能够像这样定义空列表(没有参数的操作): 或者甚至像这样(无需执行任何操作): 当我尝试使用这些空列表时,会出现以下异常: Caused by: org.sp
@ConfigurationProperties
将配置属性从YAML文件绑定到java属性类
在properties类中有以下字段:
private List<Map<String, Map<String, String>>> actionsBeforeIndexing;
这很有效。但我也希望能够像这样定义空列表(没有参数的操作):
或者甚至像这样(无需执行任何操作):
当我尝试使用这些空列表时,会出现以下异常:
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.util.List<java.util.Map<java.lang.String, java.util.Map<java.lang.String, java.lang.String>>>]
原因:org.springframework.core.convert.ConverterNotFoundException:未找到能够从类型[java.lang.String]转换为类型[java.util.List]的转换器
如何让它工作?也许有一种方法可以像列表一样强制解析YAML选项,即使没有定义列表?不是很好,但有效的解决方案是创建两个类型转换器:
/**
*将YAML中的空属性转换为空映射。
*
*分析如下:
*
*重新编制索引之前的操作:-->映射
*/
@组成部分
@配置属性绑定
公共类ActionListPropertyConverter实现转换器{
@凌驾
公共映射转换(字符串源){
Map Map=newhashmap();
if(StringUtils.hasText(源)){
/*
*不以冒号结尾分析操作名称:
*
*重新编制索引前的操作:
*-testAction-->映射
*/
put(源代码,newhashmap());
}
返回图;
}
}
/**
*将YAML中的空属性转换为空映射。
*
*分析如下:
*
*重新编制索引前的操作:
*-testAction:-->映射
*/
@组成部分
@配置属性绑定
公共类ActionParameterPropertyConverter实现转换器{
@凌驾
公共映射转换(字符串源){
返回新的HashMap();
}
}
actionsBeforeIndexing:
- testAction
actionsBeforeIndexing:
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.util.List<java.util.Map<java.lang.String, java.util.Map<java.lang.String, java.lang.String>>>]