Jackson, Retrofit, JodaTime deserialization
First View Analysis:
There are some issues in your code. That your provided time "/Date(1461208761970+0000)/"
not looking familiar. You have some code issue also. That your constructor can not talk with joda constructor properly
Root Cause:
Your issue:
com.fasterxml.jackson.databind.JsonMappingException: Class com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer has no default (no arg) constructor
is generates from your code:
at br.com.soutsapp.user.souts.WCFClientTest.getPaymentHistoryTest(WCFClientTest.java:96)
Issue Analysis:
To deserialize value to org.joda.time.DateTime
you have to define @JsonDeserialize
because Jackson cannot figure out what method/constructor use from org.joda.time.DateTime
to initialize it from a string value.
Solution - 1:
You need to use serialize as well as deserialize. You should check jar availability and should set to reading type as JSON.
You can follow these 3 steps.
- You need to use serialize as well as deserialize. Your date patter should look like as JsonFormat.
Code will be
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")@JsonSerialize(using = DateTimeSerializer.class)@JsonDeserialize(using = DateTimeDeserializer.class)private DateTime paymentDate;
Make sure that you have on classpath
jackson-datatype-joda
.Change SSM configuration to use JSON format by default (indefaultMemcachedClient definition):
<property name="defaultSerializationType" value="JSON" />
Resource Link:
https://github.com/ragnor/simple-spring-memcached/issues/41
Solution - 2:
You can add another constructor also. That may effect.
public PaymentHistoryItemApiResult() { super(); }
Resource Link: Following 2 links containing more error analysis with solution
- Jackson Exceptions – Problems and Solutions
- Jackson – JsonMappingException (No serializer found for class)
Another Solution:
If you don't want to use deserialize then you can follow details:
@JsonDeserialize
expects a JsonDeserializer
with a no-arg constructor. The most recent version of DateTimeDeserializer does not have such a constructor.
If you've fixed the format, ie. yourTimestamp should just be a timestamp, then you could simply register the JodaModule
with the ObjectMapper
. It will use DateTimeDeserializer internally for DateTime fields. You can get rid of the @JsonDeserialize annotations.
mapper.registerModule(new JodaModule());
You'll need to add the jackson-datatype-joda
library.
Resource link:
For null value serialization and deserialization
If you don't want to serialize null values, you can use the following setting during serialization:
objectMapper.setSerializationInclusion(Include.NON_NULL);
For deserialization Jackson should ideally be able to handle null values in the serialized output.
Credit goes to @jackall
or you can use annotations also in your class.
@JsonIgnoreProperties(ignoreUnknown = true)@JsonInclude(Include.NON_NULL)
I use my custom deserialize:
@JsonDeserialize(using = MyDateTimeDeserializer.class)private DateTime expiryTimeFirebaseCustomAccessToken;
with class:
import com.fasterxml.jackson.datatype.joda.cfg.FormatConfig;import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat;import com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer;import org.joda.time.DateTime;/** * Created by rudi on 3/28/18. */public class MyDateTimeDeserializer extends DateTimeDeserializer { public MyDateTimeDeserializer() { super(DateTime.class, FormatConfig.DEFAULT_DATETIME_PARSER); } public MyDateTimeDeserializer(Class<?> cls, JacksonJodaDateFormat format) { super(cls, format); }}
and it's works for me... Maybe it can contribute
Just use https://github.com/FasterXML/jackson-datatype-joda. It works out of the box and you don't have to use additional annotations to serialize/deserialize datetime into joda time objects.