将 ConfigurationProperties 绑定到 <Enum,Pojo> 的映射

Aks*_*ngh 5 java spring-boot

描述 我正在尝试将以下配置与我的 Component 类绑定 -

platform:
  service:
    config:
      guard:
       hostname: fancy-host1.kiki.com
       resources:
         - name: bark
           api-path: dog/alert/bark/{dog-id}
         - name: bite
           api-path: dog/alert/bite/{dog-id}
           json-path: $..kill-mode
      play:
        hostname: fancy-host2.kiki.com
        resources:
         - name: lick
           api-path: dog/chill/lick/{dog-id}
           json-path: $..cute-mode
Run Code Online (Sandbox Code Playgroud)

我的组件类看起来像这样 -

@Component
@ConfigurationProperties(prefix = "platform.service")
public class DogConfig
{
    @Getter
    @Setter
    public class Resource
    {
        private String name;
        private String apiPath;
        private String jsonPath;
    }

    @Getter
    @Setter
    public class APIConfig
    {
        private String hostname;
        private List<Resource> resources = new ArrayList<>();
    }

    private Map<ServiceType, APIConfig> config = new LinkedHashMap<>();

    public Map<ServiceType, APIConfig> getConfig()
    {
        return config;
    }

    public void setConfig(Map<ServiceType, APIConfig> config)
    {
        this.config = config;
    }
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,ServiceType 是一个枚举值 GUARD 和 PLAY。

问题 虽然我的 Spring Boot 应用程序在初始化时没有抛出任何错误,但它没有将我的 YAML 绑定到 DogConfig 类。我不确定我到底错过了什么。

到目前为止,我的故障排除工作 依赖于这个 spring 文档,将我的配置具体化。我知道@ConfigurationProperties 是类型安全的,并且已经单独测试了枚举、映射和 POJO 的绑定。但是同时拥有所有三个是我无法实现的。

cle*_*ion 2

请在 Resource 和 APIConfig 的内部类中添加static,例如:

public static class Resource {
    private String name;
    private String apiPath;
    private String jsonPath;
}
Run Code Online (Sandbox Code Playgroud)