本地
public static void main(String[] args)throws Exception {
String workingDir = System.getProperty("user.dir");
String filePath = workingDir+"\\src\\main\\resources\\application.yml";
FileInputStream in = new FileInputStream(filePath);
Yaml yaml = new Yaml();
Map<String, Object> result = yaml.load(in);
Map<String, Object> serverConfig = (Map<String, Object>) result.get("config");
String port = serverConfig.get("swagger-version").toString();
System.out.println("Port: " + port);
}
服务器
public static void main(String[] args) throws Exception {
// 获取 application.yml 文件作为输入流
InputStream in = Main.class.getResourceAsStream("/BOOT-INF/classes/application.yml");
Yaml yaml = new Yaml(new Constructor(Map.class));
// 使用 load 方法加载 YAML 文件
Map<String, Object> result = yaml.load(in);
// 从 YAML 结果中获取配置
Map<String, Object> serverConfig = (Map<String, Object>) result.get("config");
String port = serverConfig.get("swagger-version").toString();
System.out.println("Port: " + port);
}