使用Jackson的root名称前缀进行YAML的选择

发布于 2025-01-25 17:14:38 字数 864 浏览 1 评论 0 原文

我想使用Jackson和Jackson-Dataformat-Yaml将YAML文件列为Java对象。

YAML是

company:
  product:
    settings:
      property: value

目标类是

@Getter @Setter
public class TargetObject {
  private Map<String, String> settings;
}

进行挑选的代码,

  TargetObject config =
  new ObjectMapper(new YAMLFactory())
      .readerFor(TargetObject.class)
      .withRootName("company.product")
      .readValue(yamlResource.getInputStream());

当执行此代码时,我会遵循异常:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Root name ('company') does not match expected ('company.product') for type `a.b.c.TargetObject`

没有第二个嵌套“产品”所有内容。在不触及YAML的情况下解决此问题有可能解决吗?我已经阅读了有关逃避点作为YAML键的之类的dots,例如“ [company.product]” ,但可悲的是,在我的用例中,这不是一个选择。

问候,Rokko

I would like to deserialize a YAML file into a Java object using Jackson and jackson-dataformat-yaml.

The YAML is

company:
  product:
    settings:
      property: value

The target class is

@Getter @Setter
public class TargetObject {
  private Map<String, String> settings;
}

The code for deserializing is

  TargetObject config =
  new ObjectMapper(new YAMLFactory())
      .readerFor(TargetObject.class)
      .withRootName("company.product")
      .readValue(yamlResource.getInputStream());

I get following exception when executing this code:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Root name ('company') does not match expected ('company.product') for type `a.b.c.TargetObject`

Without the second nesting "product" everything works. Is there any possibility to solve this issue without touching the YAML? I've read about escaping dots as YAML keys like "[company.product]", but sadly that is not an option in my use case.

Regards, Rokko

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久随 2025-02-01 17:14:38

您非常接近解决方案,问题出在您使用错误的 objectreader#withRootname 代码中的方法:

TargetObject config =
  new ObjectMapper(new YAMLFactory())
      .readerFor(TargetObject.class)
      .withRootName("company.product") //<-- here the error
      .readValue(yamlResource.getInputStream());

相反,您必须使用 objectReader#at 选择您对适当“/company/product”的YAML部分的方法,以获得预期结果:

TargetObject config = 
  new ObjectMapper(new YAMLFactory())
      .readerFor(TargetObject.class)
      .at("/company/product") //<--here the new method
      .readValue(yamlResource.getInputStream());

You are very close to the solution, the problem stands in the fact you are using the wrong ObjectReader#withRootName method in your code:

TargetObject config =
  new ObjectMapper(new YAMLFactory())
      .readerFor(TargetObject.class)
      .withRootName("company.product") //<-- here the error
      .readValue(yamlResource.getInputStream());

Instead you have to use the ObjectReader#at method to select the yaml part you are interested with the appropriate "/company/product" path to obtain the expected result:

TargetObject config = 
  new ObjectMapper(new YAMLFactory())
      .readerFor(TargetObject.class)
      .at("/company/product") //<--here the new method
      .readValue(yamlResource.getInputStream());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文