当Float为null时,Jackson(JSON)抛出JsonMappingException [英] Jackson (JSON) throws JsonMappingException when Float is null

查看:289
本文介绍了当Float为null时,Jackson(JSON)抛出JsonMappingException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring(使用Jackson)和jQuery将表单作为对象传递。我的pojo包括可空的花车。但是,当字段为空时,杰克逊抛出一个执行:

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)?

谢谢

推荐答案

默认情况下,Jackson确实只将显式JSON null视为空值。但是如果需要从空String强制到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.

在短期内,您还可以注册自定义反序列化器( http://wiki.fasterxml.com/JacksonHowToCustomDeserializers 讲述一些关于它的事情,尽管不是指南)接受空String并产生null。

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.

这篇关于当Float为null时,Jackson(JSON)抛出JsonMappingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆