Binding map from config yaml

spring boot @configurationproperties map yaml
spring boot configurationproperties map yaml
spring properties map of objects
yaml map of objects
spring inject map from properties file
spring @value map
@configurationproperties yaml map
spring boot yaml list of objects

In spring boot project i am trying to bind a map from yaml file. I have tried most of the solutions to bind map but getting following error:

    Property: order.events
    Value: null
    Reason: must not be empty
 Action:
 Update your application's configuration

As you can see on config class i have getter/setter through lombok and inner class is a static class.

@Data
@Validated
@ConfigurationProperties(prefix = "order", ignoreUnknownFields = false)
public class OrderConfig {

    private String clientKey;
    private String apiVersion;
    private String endpoint;

    @Valid
    @NotEmpty
    private Map<String, Event> events;

    @Data
    public static class Event {

        @NotBlank
        private String action;
        @NotBlank
        private String eventName;
    }
}

By using lombok i already have all necessary getter and setter methods. My config yaml file is:

    order:
      clientKey: client_1
      apiVersion: 1.0.0
      endpoint: https://www.ordertest.com/api
      events:
        order.create:
          action: track
          eventName: purchase
        order.place:
          action: track
          eventName: purchase

And my Application class:

@SpringBootApplication
@EnableConfigurationProperties(OrderConfig.class)
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

If you really want your keys to be order.create and order.place then you have to specify them as "[order.create]" and "[order.place]" in order to escape the periods. Otherwise you should be able to work with just create and place.

Spring Boot Configuration Binding, Second, let's create a bean class – ServerProperties – to encapsulate the logic of binding our configuration properties to Maps: ? 1. 2. 3. 4. 5. 6. This YAML creates a ConfigMap with the value database set to mongodb, and database_uri, and keys set to the values in the YAML example code. Then, create the ConfigMap in the cluster using kubectl apply -f config-map.yaml. Using a ConfigMap in Environment Variables


You need to encode them in double quotes with square brackets Relaxed Binding

When binding to Map properties, if the key contains anything other than lowercase alpha-numeric characters or -, you need to use the bracket notation so that the original value is preserved. If the key is not surrounded by [], any characters that are not alpha-numeric or - are removed.

order:
  clientKey: client_1
  apiVersion: 1.0.0
  endpoint: https://www.ordertest.com/api
  events:
    "[order.create]":
      action: track
      eventName: purchase
    "[order.place]":
      action: track
      eventName: purchase

And also you need to annotate @Configuration on OrderConfig class

Sometimes, classes annotated with @ConfigurationProperties might not be suitable for scanning, for example, if you’re developing your own auto-configuration. In these cases, you can specify the list of types to process on any @Configuration class

Inject a Map from a YAML File with Spring, You can use properties files, YAML files, environment variables, and command-​line When binding to Map properties, if the key contains anything other than  Similar to allow configuring of groups via configuration.yaml, we should also allow it for bindings. 👍 5 Koenkk added the enhancement label Jul 10, 2019


I couldn't figured why i had that problem and wanted to give a shot with the latest Spring boot version 2.2.0 which they introduced ConstructorBinding.

I have changed my config class to following version and its fixed.

@Data
@Validated
@ConstructorBinding
@ConfigurationProperties(prefix = "order", ignoreUnknownFields = false)
public class OrderConfig {

    private final String clientKey;
    private final String apiVersion;
    private final String endpoint;

    @Valid
    @NotEmpty
    private final Map<String, Event> events;

    @Data
    @ConstructorBinding
    public static class Event {

        @NotBlank
        private final String action;
        @NotBlank
        private final String eventName;
    }
}

24. Externalized Configuration - Project Metadata API Guide, You can use properties files, YAML files, environment variables and annotation​, accessed via Spring's Environment abstraction or bound to structured objects. YAML as Properties and the YamlMapFactoryBean will load YAML as a Map . Configuration data as files. Another interesting way to consume configuration data is by pointing to a ConfigMap in the spec.volumessection of your Deployment or Pod spec.. If you have no clue what Volumes (in Kubernetes) are, don’t worry.


23. Externalized Configuration, How to map Spring Boot properties to a POJO using ConfigurationProperties Below configuration class uses @ConfigurationProperties annotation to bind and validate the external How to read values from YAML file in Spring Boot  $ oc get configmaps game-config-2 -o yaml apiVersion: v1 data: game.properties: |- enemies=aliens lives=3 enemies.cheat=true enemies.cheat.level=noGoodRotten secret.code.passphrase=UUDDLRLRBABAS secret.code.allowed=true secret.code.lives=30 ui.properties: | color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyNice kind: ConfigMap metadata: creationTimestamp: 2016-02


How to map Spring Boot properties to a POJO using , Configuration can by default be provided in either Java properties, YAML, JSON the MapFormat annotation allows you to bind a map to a single configuration  Spring Boot @ConfigurationProperties is letting developer maps the entire .properties and yml file into an object easily.. P.S Tested with Spring Boot 2.1.2.RELEASE. 1. @Value. 1.1 Normally, we use the @Value to inject the .properties value one by one, this is good for small and simple structure .properties files.


4 Application Configuration 2.0.0, To prevent binding of configuration property use @JsonIgnore on property getter String prop // dropwizard will set value from yaml public void setProp(Stirng prop) { . List, Map) implementation then binding will use base collection interface:. # Key binding configuration. Mitmproxy’s key bindings can be customized through in the ~/.mitmproxy/keys.yaml file. This file consists of a sequence of maps, with the following keys: key (mandatory): The key to bind. cmd (mandatory): The command to execute when the key is pressed. context: A list of contexts in which the key should be bound.