有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java需要POJO对象,以便使用SpringBoot自动连接下面的yml结构

应用程序。yml

countries:
  country:
    - name: kenya
      insuffbal: test101
      inactive: test101
    - name: botswana
      insuffbal: test102
      inactive: test101

上面的yml结构需要使用Bean映射到java对象

国家。爪哇

@Component
@ConfigurationProperties(prefix = "countries.ke")
public class Countries {
   //Need to map here - help me here
}

应用程序。爪哇

@SpringBootApplication
@EnableConfigurationProperties(Countries.class)
public class Application {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(Application.class, args);
        RepoProperties repoProperties = context.getBean(RepoProperties.class);
        repoProperties.print();
    }
}

共 (1) 个答案

  1. # 1 楼答案

    使用http://www.jsonschema2pojo.org/

    只需将yaml文件粘贴到那里,然后选择源类型yaml

    这是您需要定义的bean文件

    @Configuration
    @ConfigurationProperties(prefix = "countries")
    public class Countries {
    
        private List<Country> country = null;
    
        public List<Country> getCountry() {
            return country;
        }
    
        public void setCountry(List<Country> country) {
            this.country = country;
        }
    
        public static class Country {
    
            private String name;
            private String insuffbal;
            private String inactive;
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public String getInsuffbal() {
                return insuffbal;
            }
    
            public void setInsuffbal(String insuffbal) {
                this.insuffbal = insuffbal;
            }
    
            public String getInactive() {
                return inactive;
            }
    
            public void setInactive(String inactive) {
                this.inactive = inactive;
            }
        }
    }
    

    现在只需将Countries自动连接到您想要使用的任何位置