spring boot - springboot 3.4.3 @ConfigurationProperties issue, form application.yml to map not working - Stack Overflow

I tried with @Value, it works fine, but when i try to use maps and @ConfigurationProperties a got issue

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
    method: GET
    path: /v1/example/data
  another-endpoint:
    host: it.api
    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

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
    method: GET
    path: /v1/example/data
  another-endpoint:
    host: it.api
    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
Share Improve this question edited Mar 21 at 15:47 user2984483 asked Mar 21 at 15:12 user2984483user2984483 134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744344116a4569579.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信