简体   繁体   English

无法在 Spring Boot 中将 yml 属性读取为列表

[英]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.

相关问题 Spring Boot 属性 Yml/具有列表结构的属性 - Spring Boot Property Yml/Properties with List structure Spring Boot-无法读取Yaml属性文件application.yml - spring boot- unable to read yaml properties file application.yml 无法从 Java Spring Boot 项目的 application.yml 文件中读取用户定义的类列表 - Unable to read list of user defined class from application.yml file in a Java Spring Boot project Spring 引导读取 application.properties /.yml 变量,不带注释 - Spring Boot read application.properties / .yml variable without annotations Yml文件中的Spring Boot和属性数据 - Spring Boot and properties data from Yml file 无法从spring-boot中的Properties文件和Bootstrap.yml中选择值 - Unable to pick the values from Properties file and Bootstrap.yml in spring-boot 如何从 spring-boot 应用程序中的 application.yml 文件中读取属性 - How to read properties from application.yml file in spring-boot application 从 spring 启动应用程序中的 properties.yml 文件中读取复数值 - Read complex values from properties.yml file in spring boot application 如何从 spring boot jdbc 存储库中的属性或 yml 文件存储和读取 SQL 查询? - How to store and read SQL queries from properties or yml file in spring boot jdbc repository? 无法在spring启动应用程序中读取application.properties - Unable to read application.properties in spring boot application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM