正如this answer所建议的那样,这应该可以正常工作。
# application-dev.yml
gateway:
publicEndpoints:
- /api/v1/example,
- /api/v1/another-example
个字符
但是,我得到了
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'gateway.publicEndpoints' in value "${gateway.publicEndpoints}"
型
我尝试删除逗号(s)在yml文件,但得到相同的结果
我也试
@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {
@Value("${gateway.publicEndpoints}")
private String[] publicEndpoints;
型
得到了同样的例外
配置文件正确(dev
):
The following 1 profile is active: "dev"
型
Salvatore Bernardo提出的使用@ConfigurationProperties
的建议确实有帮助,但是我仍然很好奇为什么我不能在代码中成功地使用@Value
Salvatore还假设,这可能是由于@Value
似乎不支持yml
(文档does not corroborate的想法)
另一个家伙在SO had a similar problem,它似乎是由于错误的识别(我的识别是好的)
那是什么原因造成的呢?
Spring Boot 3.1.4
2条答案
按热度按时间rbl8hiat1#
对于复杂的结构(列表,Map)使用
ConfigurationProperties
。我在这里复制了https://github.com/sbernardo/spring-issues-examples/tree/main/sof-questions-77691200您的问题,并使用ConfigurationProperties
修复这个问题类似于Can't get Java see my custom YAML property in a Spring Boot app。
@Value
很适合用于简单值,例如String
值。希望这会有所帮助:)
siotufzp2#
似乎是 *.yml格式的某种问题。如果你用逗号拼写参数,像这样:
字符串
一切正常
尽管文档中说列表是在 *.yml中使用“破折号和空格”here生成的,