4

I'm using those three libraries: retrofit, jackson and jodatime, and I'm trying somethings to deserialize my object when it comes from my rest api, but I could not figure out how to solve this, here goes the json returned by rest api:

{
    "establishment": "Gold Ball Confraria",
    "idCardPayment": 0,
    "paymentDate": "/Date(1461208761970+0000)/",
    "total": 10
}

and that is the class that I've tried to deserialize my object:

public class PaymentHistoryItemApiResult {

    @JsonProperty("total")
    private double totalValue;
    @JsonProperty("establishment")
    private String establishmentName;
    @JsonDeserialize(using = DateTimeDeserializer.class)
    private DateTime paymentDate;
    private long idCardPayment;

    public PaymentHistoryItemApiResult() {

    }

    public PaymentHistoryItemApiResult(double totalValue, String establishmentName, DateTime paymentDate, long idCardPayment) {
        this.totalValue = totalValue;
        this.establishmentName = establishmentName;
        this.paymentDate = paymentDate;
        this.idCardPayment = idCardPayment;
    }
... getters/setters

}

Api endpoint:

    @GET("User.svc/{idUser}/payment/history")
    Call<PaymentHistoryItemApiResult> getPaymentHistory(@Path("idUser") long idUser);

How it's been called from Unit Test:

@Test
public void getPaymentHistoryTest(){

    Response<PaymentHistoryItemApiResult> info = null;
    try {
        info = UserRequester.userRequester.getPaymentHistory(153).execute();

        PaymentHistoryItemApiResult res = info.body();
        Assert.assertNotNull(res);

    } catch (IOException e) {
        e.printStackTrace();
    }

}

the error:

com.fasterxml.jackson.databind.JsonMappingException: Class com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer has no default (no arg) constructor
 at [Source: java.io.InputStreamReader@525b461a; line: 1, column: 1]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:251)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:269)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
    at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
    at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:477)
    at com.fasterxml.jackson.databind.ObjectReader._findRootDeserializer(ObjectReader.java:1813)
    at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1570)
    at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1183)
    at retrofit2.converter.jackson.JacksonResponseBodyConverter.convert(JacksonResponseBodyConverter.java:32)
    at retrofit2.converter.jackson.JacksonResponseBodyConverter.convert(JacksonResponseBodyConverter.java:23)
    at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:116)
    at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
    at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
    at br.com.soutsapp.user.souts.WCFClientTest.getPaymentHistoryTest(WCFClientTest.java:96)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.IllegalArgumentException: Class com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer has no default (no arg) constructor
    at com.fasterxml.jackson.databind.util.ClassUtil.createInstance(ClassUtil.java:594)
    at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.deserializerInstance(DefaultDeserializationContext.java:234)
    at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.findDeserializerFromAnnotation(BasicDeserializerFactory.java:1772)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.constructSettableProperty(BeanDeserializerFactory.java:728)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.addBeanProps(BeanDeserializerFactory.java:506)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:228)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:143)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:406)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:352)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
    ... 38 more

Thanks in advance, any help would be appreciated.

guisantogui
  • 4,017
  • 9
  • 49
  • 93
  • 1
    The Exception complains about DateTimeDeserializer not having a no-arg constructor. The constructor of DateTimeDeserializer expects a Class and a JacksonJodaDateFormat. The JsonDeserialize annotation has an optional "builder" parameter, maybe that can help you to achieve your goal. – localhorst Apr 22 '16 at 13:45
  • @localhorst I'll try something using this `JacksonJodaDateFormat`, thanks – guisantogui Apr 22 '16 at 13:59
  • I was using jackson joda time module and everything simply worked, even without declaring deserializators – Antoniossss Apr 25 '16 at 20:00

3 Answers3

4

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.

  1. 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;
  1. Make sure that you have on classpath jackson-datatype-joda.

  2. Change SSM configuration to use JSON format by default (in defaultMemcachedClient 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

  1. Jackson Exceptions – Problems and Solutions
  2. 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:

  1. joda.time.DateTime deserialization error

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)
Community
  • 1
  • 1
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
  • I guess it might be worth mentioning how to add the objectMapper to retrofit service. .addConverterFactory(JacksonConverterFactory.create(objectMapper)) – Mukiza Andrew Jul 31 '16 at 18:17
1

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

Rudi Wijaya
  • 872
  • 1
  • 10
  • 25
-1

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.

Asaph
  • 159,146
  • 25
  • 197
  • 199
Antoniossss
  • 31,590
  • 6
  • 57
  • 99