用于类型安全嵌套属性的Springboot配置类不起作用

时间:2019-04-11 06:55:36

标签: java spring-boot yaml lombok

我正在尝试使用spring-boot typesafe嵌套属性加载yaml配置文件。我想根据配置文件从配置文件轻松访问属性。

我正在使用lombok @Data注释来摆脱样板代码。在我看来,这是正确的类定义,但不起作用。我能看到的输出是。

ConfigReader(services=[ConfigReader.Services(name=some-service, baseUri=https://xxxxxx, authorization=xxx, resource=null)], userServices=null, contentType=application/json)
content-type: "application/json"
services:
  -
    name: some-service
    baseUri: 'https://xxxxxx'
    authorization: 'xxx'
    resource:
      login: 'login'

user-service:
  -
    name: someother-service
    baseUri: 'https://xxxxx'
    authorization:
      - password: "x"
      - username: "x"
  -
    name: someanother-service
    baseUri: 'https://xxxxx'
    authorization:
      - password: "x"
      - username: "x"
@ConfigurationProperties
@Configuration
@Component
@Data
public class ConfigReader {

    private List<Services> services;
    private List<UserServices> userServices;
    private String contentType;

    @Data
    static class Services {
        private String name;
        private String baseUri;
        private String authorization;
        private Resource resource;

        @Data
        static class Resource {
            private String resourceName;
        }
    }

    @Data
    static class UserServices {
        private String name;
        private String baseUri;
        private Authorization authorization;

        @Data
        static class Authorization {
            private String userName;
            private String password;
        }
    }
}

想根据所选的配置文件轻松地从yaml文件中读取这些属性。

0 个答案:

没有答案