You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Jackson解析YAML时反序列化START_OBJECT报错如何解决?

问题原因
  1. 你提供的YAML顶层结构为数组,没有外层对象包裹entries字段,但你的反序列化目标YamlProcessor是一个Java对象,要求YAML顶层为包含entries键的对象,二者结构不匹配触发反序列化异常。
  2. 你贴出的YamlProcessor类存在语法错误,ListYamlEntries>漏写了泛型左尖括号,正确写法为List<YamlEntries>。
解决方案

方案一:调整Java解析逻辑适配现有YAML(无需修改YAML)

直接将反序列化目标改为List<YamlEntries>即可,无需外层YamlProcessor封装,代码示例如下:

public List<YamlEntries> yamlParser(String file) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
    objectMapper.findAndRegisterModules();
    // 用TypeReference指定泛型类型
    List<YamlEntries> yamlEntries = objectMapper.readValue(new File(file), new TypeReference<List<YamlEntries>>() {});

    yamlEntries.forEach(valuesOfYaml -> {
        log.info("applicationName: " + valuesOfYaml.getApplicationName());
        log.info("operationId: " + valuesOfYaml.getOperationId());
    });

    return yamlEntries;
}

方案二:保留YamlProcessor封装,调整YAML结构

先修正YamlProcessor的语法错误:

@Getter
@Setter
public class YamlProcessor {
    List<YamlEntries> entries;
}

再修改YAML,增加外层entries键包裹数组:

entries:
  - operationId: "getG"
    applicationName: "c"
  - operationId: "get"
    applicationName: "c"

原有解析逻辑可直接正常运行。

内容的提问来源于stack exchange,提问作者Nicolae Marius Ghergu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 21:24:04