Jackson (JSON) 当 Float 为 null 时抛出 JsonMappingException

发布于 2024-09-15 04:17:20 字数 310 浏览 6 评论 0原文

我正在使用 Spring(与 Jackson)和 jQuery 将表单作为对象传递。我的 pojo 包含可为 null 的浮点数。但是,当字段为空时,Jackson 会抛出一个异常:

org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.lang.Float from String value '': not a valid double value

我必须做什么才能允许空值(或表单字段中的空值)?

谢谢 呃

I am using Spring (with Jackson) and jQuery to pass a form as an object. My pojo includes nullable floats. However, when the fields are empty, Jackson throws an Exeption:

org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.lang.Float from String value '': not a valid double value

What do I have to do in order to allow nulls (or empty values in the form field)?

Thanks
Er

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

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

发布评论

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

评论(2

醉态萌生 2024-09-22 04:17:20

默认情况下,Jackson 确实只将显式 JSON null 视为 null 值。但是,如果需要从空字符串强制转换为 null,则可以轻松添加功能请求。实际上,这听起来像是一个不错的改进——这就是开源项目经常被改进的方式,基于用户对他们认为应该存在的东西、事情应该工作的方式的要求。

在短期内,您还可以注册自定义反序列化器(http://wiki.fasterxml.com/JacksonHowToCustomDeserializers 告诉有关它的一些信息,尽管不是指南)接受空字符串并生成 null。

By default Jackson does indeed only consider explicit JSON null as null value. But if coercion from empty String to null was desired, it's easy to add feature requests. It sounds like a nice improvement actually -- this is how open source projects are often improved, based on user's asking for things they think should be there, ways things should work.

On short term you could also register custom deserializer (http://wiki.fasterxml.com/JacksonHowToCustomDeserializers tells something about it, although is not a guide) that accepts empty String and produces null.

惜醉颜 2024-09-22 04:17:20

最好的办法是检查 javascript 代码中是否有空字符串,并在这种情况下传递 null

Your best bet is to check for empty string in the javascript code and pass null in that case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文