我有一个带有字段的模型:
@JsonDeserialize(as = DateTimeDeserializer.class)
private DateTime expiryTimeFirebaseCustomAccessToken;
使用env:
compile('com.fasterxml.jackson.datatype:jackson-datatype-joda:2.9.4')
compile('com.squareup.retrofit2:converter-jackson:2.4.0')
compile 'joda-time:joda-time:2.9.9'
compile 'com.squareup.retrofit2:retrofit:2.4.0'
和java代码:
final JodaModule jodaModule = new JodaModule();
jodaModule.addDeserializer(DateTime.class,
DateTimeDeserializer.forType(DateTime.class));
jodaModule.addDeserializer(ReadableDateTime.class,
DateTimeDeserializer.forType(ReadableDateTime.class));
jodaModule.addDeserializer(ReadableInstant.class,
DateTimeDeserializer.forType(ReadableInstant.class));
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(jodaModule);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(serverAddress) .addConverterFactory(JacksonConverterFactory.create(objectMapper))
.build();
和json的回应:
"expiryTimeFirebaseCustomAccessToken": "2018-03-27T10:44:06.590Z"
但是,我收到了这个错误:
无法缩小类型[simple type,class org.joda.time.DateTime] 注释(值 com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer),来自 ' setExpiryTimeFirebaseCustomAccessToken':Class com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer没有 [simple type,class org.joda.time.DateTime
的子类型
答案 0 :(得分:2)
我找到了解决方案:
@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);
}
}