How to retrieve a part of yaml configuration file in Spring Boot 2.3?

Viewed 150

I have an external application.yaml configuration file which has properties which are not known to me in advance. For example:

prop0:
  prop1: 1234
  prop2:
    prop3: "Hello"
    prop4: [foo, bar, foo, bar, foo, bar]
    prop5: 
      - val1
      - val2
      - val3

prop6:
  prop7: 123
  prop8: 321

My task is to retrieve everything from prop0 in yaml format:

prop1: 1234
prop2:
  prop3: "Hello"
  prop4: [foo, bar, foo, bar, foo, bar]
  prop5: 
    - val1
    - val2
    - val3

I need to retrieve it ether as a String, InputStream or Map object where each value represents either primitive type, map, array. or list I.e.

Map<String, Object> prop0Value = ...;
String prop3Value = prop0Value.get("prop2").get("prop3");
List<String> prop5Value = prop0Value.get("prop2").get("prop5");

Notice, that spring uses its own yaml conversion mechanism where it converts lists to separate properties like:

prop0.prop2.prop5[0]=foo
prop0.prop2.prop5[1]=bar
prop0.prop2.prop5[2]=foo
...

But that format isn't correct yaml format and cannot be parsed by most tools.

Update: I was thinking about using @ConfigurationProperties annotation to resolve properties. Something like:

@Configuration
@ConfigurationProperties(prefix = "prop0")
public class MyConfigProperties {

    private Map<String, String> prop2;

    public Map<String, String> getProp2() {
        return prop2;
    }

    public void setProp2(Map<String, String> prop2) {
        this.prop2 = prop2;
    }
}

But it has 2 drawbacks:

  1. It returns properties under prop2 but I need all properties under prop0. As I said, I don't know property names which will be used in advance.
  2. It still uses Spring format to resolve lists which cannot be accepted by 3rd party libraries which expect yaml format.
0 Answers
Related