[英]Unable to read yml properties as List in Spring Boot
I am trying to read a list of servers in my Spring Boot app using @ConfigurationProperties
.我正在尝试使用
@ConfigurationProperties
在我的 Spring Boot 应用程序中读取服务器列表。 However I am getting below error.但是我得到了错误。 Is there something I am doing wrong in terms of syntax in
yaml
?在
yaml
的语法方面我做错了什么吗?
ApplicationServer应用服务器
@Component
@ConfigurationProperties(prefix = "application")
public class ApplicationServer {
List<Server> servers;
public List<Server> getServers() {
return servers;
}
public void setServers(List<Server> servers) {
System.out.println("setter called "+servers);
this.servers = servers;
}
}
Server服务器
@Component
public class Server {
String ip;
String path;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
DemoApplication演示应用
@SpringBootApplication
@EnableConfigurationProperties(ApplicationServer.class)
public class DemoApplication implements CommandLineRunner {
@Autowired
ApplicationServer applicationServer;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(applicationServer.servers);
}
}
application.yml应用程序.yml
application:
servers:
- ip:123
path:xyz
- ip:456
path:abc
- ip:789
path:pqrs
ERROR错误
Property: application.servers[0]
Value: ip:123 path:xyz
Origin: class path resource [application.yml] - 3:11
Reason: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [com.example.demo.Server]
application.yaml modified as: application.yaml修改为:
application:
servers:
- {ip: 123,path: xyz}
- {ip: 456,path: abc}
- {ip: 789,path: pqrs}
or或者
application:
servers:
- ip: 123
path: xyz
- ip: 456
path: abc
- ip: 789
path: pqrs
Space required after colon冒号后需要空格
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.