gpt4 book ai didi

java - 使用 jackson databind 解析 yaml 会导致 MismatchedInputException

转载 作者:行者123 更新时间:2023-12-02 10:34:31 24 4
gpt4 key购买 nike

我试图读取的yaml格式如下

- name: storage
scm: git
src: git+https://bitbucket.org/cer/linux_storage_jenkins.git
version: '5'
- name: linux_jenkins
scm: git
src: git+https://bitbucket.org/cer/linux_jenkins.git
version: '8'

我的模型很简单

public class Requirementsyml {
//setter and getter in place
private List<Requirement> items;
}

public class Requirement {
private String name;
private String scm;
private String src;
private String version;
}

我正在尝试读取 yaml 内容,如下所示:

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Requirementsyml req = mapper.readValue(yaml_file_content, Requirementsyml.class);

但是我收到以下异常com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_ARRAY token 中反序列化 Demandyml 实例

由于模型有一个列表,我认为输入不应不匹配。我尝试将 yaml 文件提供为 java 文件对象,而不是其内容作为字符串。结果保持不变。我错过了什么?

最佳答案

yaml 结构应该有 items 数组/列表

items:
- name: storage
scm: git
src: git+https://bitbucket.org/cer/linux_storage_jenkins.git
version: '5'
- name: linux_jenkins
scm: git
src: git+https://bitbucket.org/cer/linux_jenkins.git
version: '8'

或者您可以将其视为需求列表,无需更改 yaml

@Test
void test() throws IOException {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
List<Requirement> requirements = mapper.readValue(new File("src/test/resources/requirements.yml"), new TypeReference<List<Requirement>>() {});
assertEquals(2, requirements.size());
}

还要确保 Requirement 和其他类没有参数构造函数,否则在 ObjectMapper 上设置适当的标志

关于java - 使用 jackson databind 解析 yaml 会导致 MismatchedInputException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53386476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com