gpt4 book ai didi

java - 如何通过java解析yaml文件(snakeYaml)

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

我想解析 config.yaml 文件,但遇到了一些问题。
这是我的 JavaBean 代码

Oss.class:

import lombok.Data;

import java.util.List;

/**
* Created by Gavin
* on 2019/11/15 21:06
*/
@Data
public class Oss {
private String server;
private List<OssConfig> configList;
}

OssConfig.Class:


import lombok.Data;

/**
* Created by Gavin
* on 2019/11/16 9:32
*/
@Data
public class OssConfig {
private String endPoint;
private String accessKey;
private String accessKeySecret;
private String bucketName;
}

这是我的 yaml 文件

server: aliyun
configList:
-
endpoint: "http://oss-cn-hangzhou.aliyuncs1.com"
bucketName: "<yourBucketName>"
accessKeyId: "<yourAccessKeySecret>"
accessKeySecret: "<yourAccessSecret>"
-
endpoint: "http://oss-cn-hangzhou.aliyuncs2.com"
bucketName: "<yourBucketName>"
accessKeyId: "<yourAccessKeySecret>"
accessKeySecret: "<yourAccessSecret>"
-
endpoint: "http://oss-cn-hangzhou.aliyuncs3.com"
bucketName: "<yourBucketName>"
accessKeyId: "<yourAccessKeySecret>"
accessKeySecret: "<yourAccessSecret>"

测试代码:

import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

/**
* Created by Gavin
* on 2019/11/16 9:05
*/
public class YamlTest {
public static void main(String[] args) {
Constructor constructor = new Constructor(Oss.class);
TypeDescription ossDescription = new TypeDescription(Oss.class);
ossDescription.putListPropertyType("configList",OssConfig.class);
constructor.addTypeDescription(ossDescription);
Yaml yaml = new Yaml(constructor);
Oss load = (Oss) yaml.load(YamlTest.class.getClassLoader().getResourceAsStream("config.yaml"));
System.out.println(load);


}

控制台打印:

Exception in thread "main" mapping values are not allowed here
in 'reader', line 2, column 13:
configList:
^

at org.yaml.snakeyaml.scanner.ScannerImpl.fetchValue(ScannerImpl.java:871)
at org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:360)
at org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:226)
at org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingKey.produce(ParserImpl.java:557)
at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:157)
at org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:147)
at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:224)
at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:155)
at org.yaml.snakeyaml.composer.Composer.composeDocument(Composer.java:122)
at org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:105)
at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:120)
at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:450)
at org.yaml.snakeyaml.Yaml.load(Yaml.java:381)
at com.lew.scott.screencapture.YamlTest.main(YamlTest.java:18)

Process finished with exit code 1

我读了snakeYaml文档,但我不知道为什么它不能工作?如果没有层级关系,会解决(但不是必须的,希望用户可以多写一些配置)

最佳答案

错误的yaml语法,猜猜你想要的是

server: aliyun
configList:
-
endpoint: "http://oss-cn-hangzhou.aliyuncs1.com"
bucketName: "<yourBucketName>"
accessKeyId: "<yourAccessKeySecret>"
accessKeySecret: "<yourAccessSecret>"

Yaml online editor

关于java - 如何通过java解析yaml文件(snakeYaml),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58887019/

25 4 0