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
1 Answer
Reset to default 0For 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条)