gpt4 book ai didi

java - 如何将复杂的YAML解析为Java对象层次结构(Spring Boot配置)

转载 作者:行者123 更新时间:2023-12-02 09:14:54 25 4
gpt4 key购买 nike

我需要定义服务连接属性,这些属性将用于配置用于对 Spring Boot 服务进行 REST 端点调用的 RestTemplate

注意:将来可能会涉及服务发现机制,但我已被指示暂时采用该途径。

服务连接属性:

  • 连接方案 - http/https
  • 主机名
  • 主机端口
  • 连接超时
  • 请求超时
  • 套接字超时
  • 默认保持 Activity 状态
  • 每条路由的默认最大连接数
  • 最大总连接数

可能的配置(YAML):

services-config:
global:
scheme: http|https
connectionTimeout: <timeout ms>
requestTimeout: <timeout ms>
socketTimeout: <timeout ms>
defaultKeepAlive: <timeout ms>
defaultMaxConnPerRoute: <count>
maxTotalConnections: <count>
services:
- id: <ID> [?]
name: <name> [?]
host: localhost|<host>
port: <port>
- id: <ID> [?]
name: <name> [?]
host: localhost|<host>
port: <port>
...

目的是将此信息放入 application.yml 文件中,以便可以在 Spring 应用程序中访问它。

有没有办法将 YAML 格式的信息提取到 Java 对象层次结构中,如下所示:

class : ServicesConfig
global : GlobalConnectionProperties
services : map<String, ServiceConnectionProperties>

class : GlobalConnectionProperties
scheme : String (maybe enum instead)
connectionTimeout : int
requestTimeout : int
socketTimeout : int
defaultKeepAlive : int
defaultMaxConnPerRoute : int
maxTotalConnections : int

class : ServiceConnectionProperties
id : String
name : String
host : String
port : int

更新:

尝试了以下方法并取得了部分成功:

服务配置.java

@Configuration
public class ServiceConfiguration {

private ServicesConfig servicesConfig;

@Autowired
public ServiceConfiguration(ServicesConfig servicesConfig) {
this.servicesConfig = servicesConfig;
System.out.println(servicesConfig);
}

}

ServicesConfig.java:

@ConfigurationProperties(value = "services-config")
@EnableConfigurationProperties(
value = {GlobalConnectionProperties.class, ServiceConnectionProperties.class})
public class ServicesConfig {

private GlobalConnectionProperties globalProps;
private Map<String, ServiceConnectionProperties> services;

public GlobalConnectionProperties getGlobalProps() {
return globalProps;
}

public void setGlobalProps(GlobalConnectionProperties globalProps) {
this.globalProps = globalProps;
}

public Map<String, ServiceConnectionProperties> getServices() {
return services;
}

public void setServices(Map<String, ServiceConnectionProperties> services) {
this.services = services;
}

@Override
public String toString() {
return "ServicesConfig [globalProps=" + globalProps + ", services=" + services + "]";
}

}

application.yml:

services-config:
global:
scheme: http
connectionTimeout: 30000
requestTimeout: 30000
socketTimeout: 60000
defaultKeepAlive: 20000
defaultMaxConnPerRoute: 10
maxTotalConnections: 200
services:
- id: foo
name: foo service
host: 192.168.56.101
port: 8090
- id: bar
name: bar service
host: 192.168.56.102
port: 9010

GlobalConnectionProperties 和 ServiceConnectionProperties 类已被省略,因为它们仅包含属性、getter/setter 方法和 toString()。

当我运行服务时,控制台上会显示以下内容:

ServicesConfig [globalProps=null, services={0=ServiceConnectionProperties [id=foo, name=foo service, host=192.168.56.101, port=8090], 1=ServiceConnectionProperties [id=bar, name=bar service, host=192.168.56.102, port=9010]}]

我实际上很惊讶列表项被解析并插入到 map 中。我希望“id”成为键而不是默认整数。奇怪的是“全局”属性没有进入 map 。

我需要更正什么才能读取全局对象项?

有没有办法通过项目“id”属性来设置 map 条目的键?

最佳答案

What do I need to correct to get the global object items read in?

属性的绑定(bind)遵循 Java Bean 约定,因此在 ServicesConfig 中,您需要将字段和 getters/setters 从 globalProps 重命名为 global

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-typesafe-configuration-properties评论中的链接有更多详细信息。

Is there a way to key the map entries by the item "id" property?

对于映射条目,您的 yaml 语法不正确。请参阅使用 :? 的语法,地址:https://bitbucket.org/asomov/snakeyaml/wiki/Documentation#markdown-header-type-safe-collections 。你的 map 将是

services:
? foo
: { id: foo,
name: foo service,
host: 192.168.56.101,
port: 8090 }
? bar
: { id: bar,
name: bar service,
host: 192.168.56.102,
port: 9010}

关于java - 如何将复杂的YAML解析为Java对象层次结构(Spring Boot配置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59076828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com