spring-bootyamlconfigurationproperties

springboot 3.4.3 @ConfigurationProperties issue, form application.yml to map not working


I tried with @Value, it works fine, but when i try to use maps and @ConfigurationProperties a got issue, always empty or null depends of which test I done

application.yml file:

endpoints:
  first-enpoint:
    host: it.api.com
    method: GET
    path: /v1/example/data
  another-endpoint:
    host: it.api.com
    method: POST
    path: /v2/example/data

java class:

@Component
@ConfigurationProperties(prefix = "endpoints")
@Data
@Slf4j
public class EndpointsProperties {

    private Map<String, Endpoint> endpoints;

    @PostConstruct
    public void init() {
        log.info("loaded endpoints from application.yml: {}", endpoints);
    }

    @Data
    public static class Endpoint {
        private String host;
        private String method;
        private String path;
    }
}
result:
2025-03-21T16:43:39.524+01:00  INFO 25712 --- [middleware] [           main] c.s.m.c.httpclient.EndpointsProperties   : loaded endpoints from application.yml: null

Solution

  • For a top level property like endpoints, you can't have both a prefix and the field name. Your code assumes the property to be endpoints.endpoints.first-enpoint...

    Simply remove it from the @ConfigurationProperties annotation

    @ConfigurationProperties
    @Data
    @Slf4j
    public class EndpointsProperties {
    
        public Map<String, Endpoint> endpoints;