简体   繁体   English

YAML Jackson - 映射Integer属性时出现NullPointerException

[英]YAML Jackson - NullPointerException when mapping an Integer property

Jackson 2.6.5 杰克逊2.6.5

I try to map a YAML file to a POJO which contains Integer properties, when it reads the numeric properties, it throws: 我尝试将YAML文件映射到包含Integer属性的POJO,当它读取数字属性时,它会抛出:

com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) com.fasterxml.jackson.databind.JsonMappingException :( was java.lang.NullPointerException)

Funny thing is that if I single quote the numeric properties in the file, it parses them just fine (I guess it casts the String to Integers) 有趣的是,如果我单引号引用文件中的数字属性,它就会解析它们(我想它会将String转换为整数)

Is there any flag I have to set for Jackson to not try to cast numeric values or something? 是否有任何旗帜我必须为杰克逊设置不试图投射数值或什么?

Thank you 谢谢

I meet the same problem. 我遇到了同样的问题。

To import YAMLFactory, I add the follow file in pom.xml, 要导入YAMLFactory,我在pom.xml中添加了以下文件,

        <dependency>
            <groupId>com.aceevo.ursus</groupId>
            <artifactId>ursus-example-application</artifactId>
            <version>0.2.7</version>
        </dependency>

but the correct one is the follow, 但正确的是以下,

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
            <version>2.9.2</version>
        </dependency>

so, I change this file and the problem dispear. 所以,我改变了这个文件,问题就消失了。

I found I was having the same issue. 我发现我遇到了同样的问题。

I tried a simple yaml file in a separate project and things worked as expected (none of the fields no matter the type needed quoting!) so I'm not entirely sure why this might be happening. 我在一个单独的项目中尝试了一个简单的yaml文件,事情按预期工作(无论哪个字段都需要引用!)所以我不完全确定为什么会发生这种情况。

However after a bit of fighting with the POM and the Yaml I think this is now working and I think it is because I cleaned up the full (production) yaml to remove the different types of quotes we were using - there was a mix of ' and " and I removed all quoting where possible and then things started working again. 然而,在与POM和Yaml进行了一些战斗后,我认为现在正在运行,我认为这是因为我清理了完整的(生产)yaml以删除我们正在使用的不同类型的引号 - 有混合的'并且“我尽可能删除所有引用,然后事情又开始了。

You may also need to enable "ALLOW_UNQUOTED_FIELD_NAMES": 您可能还需要启用“ALLOW_UNQUOTED_FIELD_NAMES”:

mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);

but I'm not convinced that is what made it work nor why given it is quoted values! 但我不相信这是什么使它工作,也不是为什么给它引用价值!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM