如何在 YAML 文件中为简单的 POJO 定义映射?
How to define a map in a YAML file for simple POJO?
我正在使用 SnakeYAML 将某些 configuration/property 值解析为配置对象(定义如下)。
我的 YAML 文件如下所示:
# Thread
batchLimit: 1000
threadCountLimit: 2
# Some more config
key: value
# Map
keyMapping: <What goes here?>
我的配置 class 如下所示:
public class Configuration{
int batchlimit;
int threadCountLimit;
...
Map<String,String> keyMapping;
}
如何在YAML文件中定义keyMapping
使其可以被SnakeYAML直接解析?
这是它的样子:
#MAP
keyMapping:
key1: value1
key2: value2
一般YAML格式都有键值对的天然支持。
查看以下教程(仅作为示例):https://github.com/Animosity/CraftIRC/wiki/Complete-idiot's-introduction-to-yaml
或 google "yaml map" 了解更多详情。
具有键值对的 Yaml 文件 "AppParams.yml":
someConfig:
key1: value1
key2: value2
POJO:
public class ApplicationParameters {
private Map<String, String> someConfig;
public ApplicationParameters() {
}
public Map<String, String> getSomeConfig() {
return someConfig;
}
public void setSomeConfig(Map<String, String> someConfig) {
this.someConfig = someConfig;
}
}
Reader:
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
File paramFile = new File("AppParams.yml");
ApplicationParameters applicationParameters = mapper.readValue(paramFile, ApplicationParameters.class);
Map<String, String> someConfig = applicationParameters.getSomeConfig();
String key1Value = someConfig.get("key1"); //returns "value1"
上面的例子在POM.xml中使用了这些依赖:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.9.8</version>
</dependency>
</dependencies>
我正在使用 SnakeYAML 将某些 configuration/property 值解析为配置对象(定义如下)。
我的 YAML 文件如下所示:
# Thread
batchLimit: 1000
threadCountLimit: 2
# Some more config
key: value
# Map
keyMapping: <What goes here?>
我的配置 class 如下所示:
public class Configuration{
int batchlimit;
int threadCountLimit;
...
Map<String,String> keyMapping;
}
如何在YAML文件中定义keyMapping
使其可以被SnakeYAML直接解析?
这是它的样子:
#MAP
keyMapping:
key1: value1
key2: value2
一般YAML格式都有键值对的天然支持。 查看以下教程(仅作为示例):https://github.com/Animosity/CraftIRC/wiki/Complete-idiot's-introduction-to-yaml
或 google "yaml map" 了解更多详情。
具有键值对的 Yaml 文件 "AppParams.yml":
someConfig:
key1: value1
key2: value2
POJO:
public class ApplicationParameters {
private Map<String, String> someConfig;
public ApplicationParameters() {
}
public Map<String, String> getSomeConfig() {
return someConfig;
}
public void setSomeConfig(Map<String, String> someConfig) {
this.someConfig = someConfig;
}
}
Reader:
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
File paramFile = new File("AppParams.yml");
ApplicationParameters applicationParameters = mapper.readValue(paramFile, ApplicationParameters.class);
Map<String, String> someConfig = applicationParameters.getSomeConfig();
String key1Value = someConfig.get("key1"); //returns "value1"
上面的例子在POM.xml中使用了这些依赖:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.9.8</version>
</dependency>
</dependencies>