javaspringyamlmicronautconfigurationproperties

Micronaut @ConfigurationProperties not loading in a List of pojos correctly


I have the following property in my application.yml file:

payment-routing-config:
  drivers:
    - id: 'ANETC'
      weight: 80
      backups: ['EPAYC', 'NMIC', 'ANETD']
    - id: 'EPAYC'
      weight: 80
      backups: ['ANETC', 'NMIC', 'ANETD']
    - id: 'NMI'
      weight: 80
      backups: ['EPAYC', 'ANETC', 'ANETD']

And the following Java:

@ConfigurationProperties("paymentRoutingConfig")
public record PaymentRoutingConfig(
    List<PaymentDriver> drivers
) { }

record PaymentDriver(
    String id,
    int weight,
    List<String> backups
) { }

When I use @Inject to load in the config, the drivers property is an empty list. If I change the List drivers to List drivers I populates the following object:

PaymentRoutingConfig[drivers=[{id=ANETC, weight=80, backups=[EPAYC, NMIC, ANETD]}, {id=EPAYC, weight=80, backups=[ANETC, NMIC, ANETD]}, {id=NMI, weight=80, backups=[EPAYC, ANETC, ANETD]}]]

This is how i inject it into my controller:

@Inject
PaymentRoutingConfig paymentRoutingConfig;

Are complex objects unable to be used with @ConfigurationProperties?

I even tried simplifying List to a single object:

@ConfigurationProperties("paymentRoutingConfig")
public record PaymentRoutingConfig(
    PaymentDriver driver
) { }

record PaymentDriver(
    String id,
    int weight
) { }

yaml:

payment-routing-config:
  driver:
    id: 'test'
    weight: 10

And I get this error:

Message: Failed to inject value for parameter [driver] of class: com.***.paymentservice.infrastructure.PaymentRoutingConfig

Message: Error resolving property value [payment-routing-config.driver]. Property doesn't exist
Path Taken: new PaymentController() --> PaymentController.paymentRoutingConfig --> new PaymentRoutingConfig([PaymentDriver driver])
Path Taken: new PaymentController() --> PaymentController.paymentRoutingConfig --> new PaymentRoutingConfig([PaymentDriver driver])
io.micronaut.context.exceptions.DependencyInjectionException: Failed to inject value for parameter [driver] of class: com.***.paymentservice.infrastructure.PaymentRoutingConfig

Solution

  • Using the following in my yaml file I was able to populate the config object correctly:

    payment-routing-config:
      drivers: [
        {id: 'test', weight: 10, backups: ['EPAYC', 'ANETC', 'ANETD']},
        {id: 'test2', weight: 20, backups: ['ANETC', 'EPAYC', 'ANETD']}
      ]