Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping YAML List to List of Objects in Spring Boot

I have a problem similar to that described in Mapping list in Yaml to list of objects in Spring Boot except that I would like to vary the identifier of least one of the fields in my object from the corresponding key name used in YAML.

For example:

YAML File:

config:
    gateways:
        -
            id: 'g0'
            nbrInputs: 128
            nbrOutputs: 128
        -
            id: 'g1'
            nbrInputs: 128
            nbrOutputs: 128

Configuration Class:

@Configuration
@ConfigurationProperties(prefix="config")
public class GatewayConfig
{
    List<Gateway> gateways = new ArrayList<Gateway>();

    // Getter/Setter for gateways
    // ...

    public static class Gateway
    {
        private String id;

        @Value("${nbrInputs}")
        private int numInputs;

        @Value("${nbrOutputs}")
        private int numOutputs;

        // Getters and Setters
        // ...
    }
}

I was hoping that the @Value annotations would allow me to inject the corresponding property values, but this does not seem to work (injection of the 'id' field seems to work just fine).

Is there a way to do this with @Value (or any other annotation)?

Thank you.


Edit: Note that I am looking to determine whether I can force a correspondence between a YAML property and a field in the inner POJO without changing the name of either. There are several reasons why I might want to do this - e.g. I might not control the format of the YAML file and I would like to use a more descriptive identifier name in my POJO than was used by the author of the YAML file.

like image 212
Brandon E Taylor Avatar asked Mar 26 '16 05:03

Brandon E Taylor


People also ask

How do you represent a list in YAML?

Each element in the list is represented in YAML as a new line with the same indentation, starting with - followed by a space.

How do you write an array of objects in YAML?

The block sequence style of YAML uses hyphens or dashes to ( - ) to represent arrays. A hyphen ( - ) followed by white space ( ) represents an element of an array. When you enter the dashes, you need to ensure that all items are at the same indentation level. See the code example below to understand this better.


1 Answers

As Stephave Nicoll mentioned, @Value annotation has nothing to do with @ConfigurationProperties. Just name fields in the inner POJO same as in configuration file and this should work:

@Configuration
@ConfigurationProperties(prefix="config")
@EnableConfigurationProperties
public class GatewayConfig
{
    List<Gateway> gateways = new ArrayList<Gateway>();

    // Getter/Setter for gateways
    // ...

    public static class Gateway
    {
        private String id;
        private int nbrInputs;
        private int nbrOutputs;

        // Getters and Setters
        // ...
    }
}

Reaction on comment:

With plain Spring/Spring Boot, I don't think you can map fields with different names and load it to the list of gateways. There would be option to use plain @Value annotation, but your gateway count would need to be hard-coded:

@Component
public class Gateway0{
    @Value("${config.gateways[0].id}")
    private String id;

    @Value("${config.gateways[0].nbrInputs}")
    private int numInputs;

    @Value("${config.gateways[0].nbrOutputs}")
    private int numOutputs;

    // Getters and Setters
    // ...
}
like image 89
luboskrnac Avatar answered Nov 03 '22 02:11

luboskrnac