Mapping YAML List to List of Objects in Spring Boot

Viewed 19492

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.

1 Answers
Related