Search code examples
javaspring-bootyamljava-7

Spring Boot YAML configuration for a list of strings containing commas


I'm using Spring Boot version 1.5.22 because of Java 7 compatibility - legacy stuff, museum grade, don't use.

I need an array of strings containing commas in YAML config for Spring Boot. The problem is that this old Spring Boot doesn't fully support all YAML features - namely arrays are implemented a bit hacky.

For other arrays of strings (not containing commas), I use the following:

YAML:

element:
  list: >
    aaaa,
    bbbb

Java:

@Value("${element.list}")
protected String[] elementList;

However, with commas, the following either ended with an empty array, or ignored any escaping I tried and got split by all the commas:

YAML:

element:
  list: >
    aa,aa,
    bb,bb
element:
  list: >
    "aa,aa",
    "bb,bb"
element:
  list: >
    'aa,aa',
    'bb,bb'
element:
  list: >
    aa\,aa,
    bb\,bb
element:
  list: [
    "aa,aa",
    "bb,bb"
  ]
element:
  list:
    - aa,aa
    - bb,bb
element:
  list:
    - "aa,aa"
    - "bb,bb"

Solution

  • It looks like this old version of Spring Boot splits arrays by commas and ignores any escaping attempt. I ended up with the following:

    YAML (use semicolons instead of commas):

    element:
      list: >
        aa;aa,
        bb;bb
    

    Java (replace ; -> , in @PostConstruct):

    @Value("${element.list}")
    protected String[] elementList;
    
    @PostConstruct
    private void postConstruct() {
        for (int i = 0; i < elementList.length; i++) {
            elementList[i] = elementList[i].replace(";", ",");
        }
    }