Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested array configuration with Spring

Tags:

spring

I'm trying to retrieve the configuration from following yaml where I have some nested arrays

ems:
  filtered-queue:
    - filter-regular-expressions:
       - AAA*MD1
       - AAA*MD2
      destination-queue-names:
       - ems.omie1
       - ems.aws1   
    - filter-regular-expressions:
       - BBB*MD1
       - BBB*MD2
      destination-queue-names:
       - ems.omie2
       - ems.aws2

I've double checked and there a no indentation issues. The ConfigServer is reading the file properly.

My current code to retrieve the configuration is as follows

import java.util.ArrayList;
import java.util.List;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix="ems")
public class FilteredQueueConfiguration {
    @NestedConfigurationProperty
    private List<FilteredQueue> filteredQueue = new ArrayList<>();


    @Data
    public class FilteredQueue {
        private List<String> filterRegularExpressions = new ArrayList<>();
        private List<String> destinationQueuenames = new ArrayList<>();
    }   
}

In my main class I have the @EnableConfigurationProperties(FilteredQueueConfiguration.class) annotation

I have always the same exception abound Binding to target ... failed. Any clue of what I'm doing wrong?

like image 597
Elena Avatar asked Sep 03 '26 18:09

Elena


1 Answers

The inner class FilteredQueue should be static class, see the Type-safe Configuration Properties

import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix="ems")
public class FilteredQueueConfiguration {
    @NestedConfigurationProperty
    private List<FilteredQueue> filteredQueue = new ArrayList<>();


@Data
public static class FilteredQueue {
    private List<String> filterRegularExpressions = new ArrayList<>();
    private List<String> destinationQueuenames = new ArrayList<>();
     }   
 }
like image 156
Deadpool Avatar answered Sep 06 '26 08:09

Deadpool



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!