Spring Boot:使用 @Value 或 @ConfigurationProperties 从 yaml 读取列表 [英] Spring Boot: read list from yaml using @Value or @ConfigurationProperties
问题描述
我想从 yaml 文件 (application.yml) 中读取主机列表,该文件如下所示:
I want to read a list of hosts from a yaml file (application.yml), the file looks like this:
cors:
hosts:
allow:
- http://foo1/
- http://foo2/
- http://foo3/
(示例 1)
我使用的类定义了这样的值:
My class used defines the value like this:
@Value("${cors.hosts.allow}")
List<String> allowedHosts;
但由于 Spring 抱怨,读取失败:
But reading fails as Spring complains about this:
java.lang.IllegalArgumentException:无法解析占位符字符串值${cors.hosts.allow}"中的cors.hosts.allow"
java.lang.IllegalArgumentException: Could not resolve placeholder 'cors.hosts.allow' in string value "${cors.hosts.allow}"
当我像这样更改文件时,可以读取属性,但它自然不包含列表,而只有一个条目:
When I change the file like this the property can be read but naturally it does not contain the list but only one entry:
cors:
hosts:
allow: http://foo1, http://foo2, http://foo3
(我知道我可以将这些值作为单行读取并按 ","
拆分它们,但我还不想寻求解决方法)
(I know that I could read the values as a single line and split them by ","
but I do not want to go for a workaround yet)
这也不起作用(尽管我认为根据 snakeyamls docs):
This does not work either (although I think this should be valid according to snakeyamls docs):
cors:
hosts:
allow: !!seq [ "http://foo1", "http://foo2" ]
(跳过 !!seq
而只使用 [
/]
也是失败的)
(Skipping the !!seq
and just using the [
/ ]
is a failure too)
我阅读了这里的建议,其中涉及使用 @ConfigurationProperties
并将示例传输到 Java 并与您在示例 1 中看到的 yaml 文件一起使用:
I read the suggestion here which involves using @ConfigurationProperties
and transferred the example to Java and used it with the yaml file you see in Example1:
@Configuration
@EnableWebMvc
@ConfigurationProperties(prefix = "cors.hosts")
public class CorsConfiguration extends WebMvcConfigurerAdapter {
@NotNull
public List<String> allow;
...
当我运行它时,我收到了这样的投诉:
When I run this I get this complaint:
org.springframework.validation.BindException:org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult:1 错误字段允许"上的对象cors.hosts"中的字段错误:被拒绝值[空];代码 [NotNull.cors.hosts.allow,NotNull.allow,NotNull];论据[org.springframework.context.support.DefaultMessageSourceResolvable:代码 [cors.hosts.allow,allow];参数 ts [];默认消息[允许]];
org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 1 errors Field error in object 'cors.hosts' on field 'allow': rejected value [null]; codes [NotNull.cors.hosts.allow,NotNull.allow,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [cors.hosts.allow,allow]; argumen ts []; default message [allow]];
我搜索了其他方式让我的 CORS 主机可配置,并找到了这个 Spring Boot问题,但由于尚未完成,我无法将其用作解决方案.所有这些都是通过 Spring Boot 1.3 RC1 完成的
I searched for other means to have my CORS hosts configurable and found this Spring Boot issue but as this is not yet finished I can't use it as a solution. All of this is done with Spring Boot 1.3 RC1
推荐答案
在application.yml中使用逗号分隔值
use comma separated values in application.yml
corsHostsAllow: http://foo1/, http://foo2/, http://foo3/
用于访问的java代码
java code for access
@Value("${corsHostsAllow}")
String[] corsHostsAllow
我试过了,成功了;)
这篇关于Spring Boot:使用 @Value 或 @ConfigurationProperties 从 yaml 读取列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!