'Reason: The elements were left unbound

How can I load List of objects from configuration yml file in java springboot application?

I already tried several sources:

configuration-properties-in-spring-boot

spring-boot-configurationproperties-example

SpringBoot 2 the elements were left unbound

Stack: Java 11, SpringBoot 2.1.4, Lombok, configuration file in .yml format.

I tried to implement simple @Component, which will load data from configuration file.

Configuration values are:

allowed:
  - first-crossroad: ONE
    second-crossroad: TWO
    third-crossroad: TWO
    fourth-crossroad: THREE
  - first-crossroad: ONE
    second-crossroad: THREE
    third-crossroad: TWO
    fourth-crossroad: ONE

Java class for data loading is:

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "")
@Data
public class AllowedCrossroads {

  private List<CrossroadCombination> allowed;
}

Java CrossroadCombination object definition:

@Data
@Builder
@AllArgsConstructor
public class CrossroadCombination {

  private String firstCrossroad;
  private String secondCrossroad;
  private String thirdCrossroad;
  private String fourthCrossroad;
}

I expected values to be loaded during application run. But I am getting error:

Property: allowed[0].first-crossroad
    Value: ONE
    Origin: class path resource [application.yml]:644:17
    Reason: The elements [allowed[0].first-crossroad,allowed[0].fourth-crossroad,allowed[0].second-crossroad,allowed[0].third-crossroad,allowed[1].first-crossroad,allowed[1].fourth-crossroad,allowed[1].second-crossroad,allowed[1].third-crossroad,allowed[2].first-crossroad,allowed[2].fourth-crossroad,allowed[2].second-crossroad,allowed[2].third-crossroad] were left unbound.


Solution 1:[1]

(One of many) Solution:

  • add: @NoArgsConstructor

to java class:

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CrossroadCombination {

  private String firstCrossroad;
  private String secondCrossroad;
  private String thirdCrossroad;
  private String fourthCrossroad;
}

Explanation:

It is because when we create an object, we need to have an empty object and then fill it with data. This is why we need no args constructor.

Anyway solution from "@Daniel V" is also correct and thanks for that one!

Solution 2:[2]

Making The Inner class as static will work

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "")
@Data
public class AllowedCrossroads {

    private List<CrossroadCombination> allowed;

    @Data
    public static class CrossroadCombination {
        private String firstCrossroad;
        private String secondCrossroad;
        private String thirdCrossroad;
        private String fourthCrossroad;
    }
}

Solution 3:[3]

including Getters and Setters for all fields solved the issue

Solution 4:[4]

Did you try with java ResourceBundle library?

for example:

ResourceBundle configApp = ResourceBundle.getBundle("path to your configuration file");

after that you can use configApp like so:

configApp.getString("first-crossroad");

can you try that and tell me how it goes.

EDIT:

The data in your configuration file if your planning to use ResourceBundle should look like this:

allowed=[first-crossroad= ONE, second-crossroad= TWO, third-crossroad= TWO,fourth-crossroad= THREE,first-crossroad= ONE, second-crossroad= THREE,third-crossroad= TWO, fourth-crossroad= ONE]

the "[]" isn't neccesary but it makes it look more readable

then you call it like this:

configApp.getObject("allowed");

that will store it like this:

[first-crossroad= ONE, second-crossroad= TWO, third-crossroad= TWO,fourth-crossroad= THREE,first-crossroad= ONE, second-crossroad= THREE,third-crossroad= TWO, fourth-crossroad= ONE]

then you can cast this to an array or play with it like this.

Solution 5:[5]

In my case the variable name is not matched. Please check all fields names and mapping configuration key names.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Old Engineer
Solution 2 Wojciech Wirzbicki
Solution 3 Mukundhan
Solution 4
Solution 5 Saravana Kumar