@ConfigurationProperties to fill Map<String, Object>

49 Views Asked by At

I used Map<String, Object> data to make dynamic

@Configuration
@ConfigurationProperties("test")
@Data
public class Config {
  private Map<String, Object> data;
}

How can I get data as expected?

example yaml

test:
  a: a
  b: b
  c: 
    - name: name1
    - name: name2

result

{
  "a": "a",
  "b": "b",
  "c": {
    0: {
      "name": "name1"
    },
    1: {
      "name": "name2"
    }
  }
}

expected

{
  "a": "a",
  "b": "b",
  "c": [
    {
      "name": "name1"
    },
    {
      "name": "name2"
    }
  ]
}
1

There are 1 best solutions below

0
Milind Vedi On

This is because the data is store in a map. Add this data to a list and then print that list to get data in your requited format. Like this

List<Map<String, Object>> cList = new ArrayList<>();
    Map<Integer, Map<String, Object>> cMap = (Map<Integer, Map<String, Object>>) data.get("c");
    for (Map<String, Object> value : cMap.values()) {
        cList.add(value);
    }
    data.put("c", cList);
    //Now your data is in list cList