我想重新配置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(SnakeYaml)解析Yaml文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58887019/

10-11 21:33