I want to resovle config.yaml file, but I encounted some problems.
Here is my JavaBean Code
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;
}
This is my yaml file
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>"
Test Code :
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);
}
Console Print:
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
I read snakeYaml document,but I dont know Why can't it work? If there is no hierarchical relationship, that will be resolved(but that is not requirement. I hope user can write more configs)
error yaml syntax, Guess what you want is
server: aliyun
configList:
-
endpoint: "http://oss-cn-hangzhou.aliyuncs1.com"
bucketName: "<yourBucketName>"
accessKeyId: "<yourAccessKeySecret>"
accessKeySecret: "<yourAccessSecret>"