Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a map in a YAML file for simple POJO?

I am using snakeYaml to parse certain configuration/property values to a Configuration object.

My yaml file looks like this -

#Thread batchLimit: 1000 threadCountLimit: 2  #Some More Config key: value  #MAP keyMapping: <What goes here?> 

My Configuration class looks like this -

public class Configuration{   int batchlimit;   int threadCountLimit;   ...   Map<String,String> keyMapping; } 

How do I define the keyMapping in the YAML file so it reads directly through SnakeYAML?

like image 712
frugalcoder Avatar asked Dec 17 '15 07:12

frugalcoder


People also ask

How would you define a map in YAML for a POJO?

We create our POJO configuration class: @ConfigurationProperties(prefix = "t-shirt-size") public class TshirtSizeConfig { private Map<String, Integer> simpleMapping; public TshirtSizeConfig(Map<String, Integer> simpleMapping) { this. simpleMapping = simpleMapping; } //getters and setters.. }

How do you define a map in YAML?

In YAML, maps are represented using the colon ( : ) character. We can also use quotation marks ( " or ' ) to enclose the keys and values if we need to.

How do I create a YAML map?

In order to create Map instances from YAML or properties file configurations, we need a @ConfigurationProperties class. This class is required for containing all the nested Map groups. As we want to read all the properties under the group “spring”, our properties class uses it as a prefix.

How do I inject a map from a YAML file?

How to Inject a Map From a YAML File. Spring Boot has taken data externalization to the next level by providing a handy annotation called @ConfigurationProperties. This annotation is introduced to easily inject external properties from configuration files directly into Java objects.


1 Answers

Here is how it can look like:

#MAP keyMapping:      key1: value1     key2: value2 

Generally YAML format has natural support of key-value pairs. Take a look on the following tutorial (just for example): https://github.com/Animosity/CraftIRC/wiki/Complete-idiot's-introduction-to-yaml

Or just google "yaml map" for more details.

like image 121
AlexR Avatar answered Sep 23 '22 02:09

AlexR