解析动态 yaml 并使用注释将它们映射到 java 个对象
Parsing dynamic yaml and mapping them using annotations to java objects
我想解析具有动态属性的 yaml 文件。所以,我有针对不同国家/地区的 yaml 文件 -
netherlands.yml, usa.yml, uk.yml..等等
文件内容如下所示-
netherlands:
name: netherlands
type: country
location:
latitude: aaa
longitude: bbb
amsterdam:
name: amsterdam
type: city
latitude: xxx
longitude: yyy
rotterdam:
name: rotterdam
type: city
latitude: ddd
longitude: ggg
hague:
name: hague
type: city
latitude: kkk
longitude: lll
我想解析它并在我的代码中以这种方式读取它 -
@country(name="netherlands")
Country country;
country.getAmsterdam.getLatitude()
我正在使用 Springboot 和 Java11。
如何使用注释来实现这一点?我相信我需要为每个国家/地区编写自定义注释。但主要问题是每个国家/地区的城市名称都是动态的,城市的数量也会因国家/地区而异。此外,稍后可以在 yaml 中添加城市。
我能够编写代码来解析 yaml 并将其映射到我的对象。但到目前为止我看到的是,它需要映射到具有固定属性的 Class ,这不是我的情况。这是我写的代码,但它不符合我的目的。
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
File file = new File(classLoader.getResource("netherlands.yml").getFile());
ObjectMapper om = new ObjectMapper(new YAMLFactory());
Country country = om.readValue(file, Country.class);
public class Country{
private String name ;
private String type;
private Location location;
// Here I need the dynamic attributes for my cities
}
我查了很多文章,但找不到类似这样的例子。你能建议一种实现这一目标的方法吗?非常感谢您的帮助。
如果您的 .yml 文件不会在运行时更改,您可以考虑使用 @ConfigurationProperties Spring 注释。
使用它你可以像这样创建@Configuration class:
@Configuration
@Data
public class CountryConfiguration {
@Bean
@ConfigurationProperties(prefix = "netherlands")
private final Country netherlands;
...
}
您还需要使用其他配置支持,如本 answer 中所述,以允许您的 netherlands.yml 和其他人对 @ConfigurationProperties 注释可见。
关于动态字段生成 - 这是不可能的,因为 Java 是一种静态类型语言。而且它没有任何意义,因为如果您不知道城市名称,您将无法使用这些城市。所以我建议您将 .yml 文件的结构更改为:
netherlands:
name: netherlands
type: country
location:
latitude: aaa
longitude: bbb
cities:
- amsterdam:
....
如果不可能。什么时候可以考虑用private final Map<String, Object> netherlands
.
我想解析具有动态属性的 yaml 文件。所以,我有针对不同国家/地区的 yaml 文件 - netherlands.yml, usa.yml, uk.yml..等等
文件内容如下所示-
netherlands:
name: netherlands
type: country
location:
latitude: aaa
longitude: bbb
amsterdam:
name: amsterdam
type: city
latitude: xxx
longitude: yyy
rotterdam:
name: rotterdam
type: city
latitude: ddd
longitude: ggg
hague:
name: hague
type: city
latitude: kkk
longitude: lll
我想解析它并在我的代码中以这种方式读取它 -
@country(name="netherlands")
Country country;
country.getAmsterdam.getLatitude()
我正在使用 Springboot 和 Java11。 如何使用注释来实现这一点?我相信我需要为每个国家/地区编写自定义注释。但主要问题是每个国家/地区的城市名称都是动态的,城市的数量也会因国家/地区而异。此外,稍后可以在 yaml 中添加城市。 我能够编写代码来解析 yaml 并将其映射到我的对象。但到目前为止我看到的是,它需要映射到具有固定属性的 Class ,这不是我的情况。这是我写的代码,但它不符合我的目的。
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
File file = new File(classLoader.getResource("netherlands.yml").getFile());
ObjectMapper om = new ObjectMapper(new YAMLFactory());
Country country = om.readValue(file, Country.class);
public class Country{
private String name ;
private String type;
private Location location;
// Here I need the dynamic attributes for my cities
}
我查了很多文章,但找不到类似这样的例子。你能建议一种实现这一目标的方法吗?非常感谢您的帮助。
如果您的 .yml 文件不会在运行时更改,您可以考虑使用 @ConfigurationProperties Spring 注释。
使用它你可以像这样创建@Configuration class:
@Configuration
@Data
public class CountryConfiguration {
@Bean
@ConfigurationProperties(prefix = "netherlands")
private final Country netherlands;
...
}
您还需要使用其他配置支持,如本 answer 中所述,以允许您的 netherlands.yml 和其他人对 @ConfigurationProperties 注释可见。
关于动态字段生成 - 这是不可能的,因为 Java 是一种静态类型语言。而且它没有任何意义,因为如果您不知道城市名称,您将无法使用这些城市。所以我建议您将 .yml 文件的结构更改为:
netherlands:
name: netherlands
type: country
location:
latitude: aaa
longitude: bbb
cities:
- amsterdam:
....
如果不可能。什么时候可以考虑用private final Map<String, Object> netherlands
.