Jackson fails on deserializing String to Joda-Time

生来就可爱ヽ(ⅴ<●) 提交于 2020-08-19 06:28:30

问题


I'm using Spring Boot 1.5.6 with Jackson 2.8.8. When deserializing the answer of a REST call, Jackson fails with the following exception:

JSON parse error: Can not construct instance of org.joda.time.DateTime: no String-argument constructor/factory method to deserialize from String value ('2018-03-19T12:05:21.885+01:00')

It's true there is no String constructor, only an Object constructor in the DateTime object.

I included the jackson-datatype-joda dependency in my build.gradle file. These are the respective lines from the build.gradle:

compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: jacksonVersion
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: jacksonVersion
compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: jacksonVersion
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5', version: jacksonVersion
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-joda', version: jacksonVersion

Is there any additional configuration I need to do?

PS: If I put the date String into a new DateTime("2018-03-19T12:05:21.885+01:00") it works fine.

Any ideas? Cheers!


回答1:


Did you register the JodaModule module in your ObjectMapper?

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());


来源:https://stackoverflow.com/questions/49361152/jackson-fails-on-deserializing-string-to-joda-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!