我正在使用这三个库:改造,杰克逊和jodatime,我正在尝试将我的对象反序列化,当它来自我的休息api,但我无法弄清楚如何解决这个问题,这里是json由rest api返回:
{
"establishment": "Gold Ball Confraria",
"idCardPayment": 0,
"paymentDate": "/Date(1461208761970+0000)/",
"total": 10
}
这就是我试图反序列化我的对象的类:
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终点:
@GET("User.svc/{idUser}/payment/history")
Call<PaymentHistoryItemApiResult> getPaymentHistory(@Path("idUser") long idUser);
如何从单元测试中调用它:
@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();
}
}
错误:
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
在此先感谢,任何帮助将不胜感激。
答案 0 :(得分:4)
您的代码中存在一些问题。您提供的时间"/Date(1461208761970+0000)/"
看起来并不熟悉。你也有一些代码问题。你的构造函数不能正确地与joda构造函数交谈
您的问题:
com.fasterxml.jackson.databind.JsonMappingException:Class com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer没有 default(no arg)构造函数
是从您的代码中生成的:
at br.com.soutsapp.user.souts.WCFClientTest.getPaymentHistoryTest(WCFClientTest.java:96)
要将值反序列化为org.joda.time.DateTime
,您必须定义@JsonDeserialize
,因为杰克逊无法弄清楚org.joda.time.DateTime
使用哪种方法/构造函数从字符串值初始化它。
您需要使用序列化以及反序列化。您应该检查jar可用性,并应将读取类型设置为JSON。
您可以按照以下3个步骤进行操作。
代码
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
@JsonSerialize(using = DateTimeSerializer.class)
@JsonDeserialize(using = DateTimeDeserializer.class)
private DateTime paymentDate;
确保您拥有类路径jackson-datatype-joda
。
默认情况下,将SSM配置更改为使用JSON格式(in defaultMemcachedClient定义):
<property name="defaultSerializationType" value="JSON" />
https://github.com/ragnor/simple-spring-memcached/issues/41
您也可以添加其他构造函数。这可能会有效。
public PaymentHistoryItemApiResult() {
super();
}
如果您不想使用反序列化,那么您可以按照以下细节进行操作:
@JsonDeserialize
期望JsonDeserializer
使用no-arg构造函数。最新版本的DateTimeDeserializer没有这样的构造函数。
如果您已修复格式,即。 yourTimestamp应该只是一个时间戳,然后您可以只使用JodaModule
注册ObjectMapper
。它将在DateTime字段内部使用DateTimeDeserializer。 您可以删除@JsonDeserialize注释。
mapper.registerModule(new JodaModule());
您需要添加jackson-datatype-joda
库。
如果您不想序列化空值,可以在序列化期间使用以下设置:
objectMapper.setSerializationInclusion(Include.NON_NULL);
对于反序列化,理想情况下,Jackson应该能够处理序列化输出中的空值。
归功于@ jackall
或者您也可以在班上使用注释。
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(Include.NON_NULL)
答案 1 :(得分:0)
我使用自定义反序列化:
@JsonDeserialize(using = MyDateTimeDeserializer.class)
private DateTime expiryTimeFirebaseCustomAccessToken;
上课:
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);
}
}
它对我有用......也许它可以贡献
答案 2 :(得分:-2)
只需使用https://github.com/FasterXML/jackson-datatype-joda即可。它开箱即用,你不必使用额外的注释来将日期时间序列化/反序列化为joda时间对象。