Jackson (JSON) 当 Float 为 null 时抛出 JsonMappingException
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,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.
最好的办法是检查 javascript 代码中是否有空字符串,并在这种情况下传递
null
。Your best bet is to check for empty string in the javascript code and pass
null
in that case.