如何通过 java(snakeYaml) 解析 yaml 文件
How to resolve yaml files by java(snakeYaml)
我想解决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>"
我想解决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>"