我正在尝试将 Retrofit2 与 Jackson 转换器和 Joda DateTime 一起使用。我拥有的代码如下:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
retrofit = new Retrofit.Builder()
.baseUrl(RestServiceApi.BASE_URL)
.addConverterFactory(JacksonConverterFactory.create(mapper))
.client(okHttpClient)
.build();
但是没有使用 Joda 序列化器/解串器,导致
{
...........
"DateTime": {
"centuryOfEra": 20,
"dayOfMonth": 29,
"dayOfWeek": 2,
"dayOfYear": 363,
"era": 1,
"hourOfDay": 8,
.....
}
}
在不使用 ObjectMapper 的情况下使用 JacksonConverterFactory.create() 时获得相同的结果
.addConverterFactory(JacksonConverterFactory.create())
如果直接使用相同的 ObjectMapper 将值写入 String,则一切都按预期工作。
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
String payload = mapper.writeValueAsString(message);
任何帮助将不胜感激。
编辑: 依赖:
+--- com.squareup.okhttp:okhttp:2.7.0
| \--- com.squareup.okio:okio:1.6.0
+--- com.squareup.okhttp:logging-interceptor:2.7.0
| \--- com.squareup.okhttp:okhttp:2.7.0 (*)
+--- com.squareup.retrofit:retrofit:2.0.0-beta2
| \--- com.squareup.okhttp:okhttp:2.5.0 -> 2.7.0 (*)
+--- com.squareup.retrofit:converter-jackson:2.0.0-beta2
| +--- com.squareup.retrofit:retrofit:2.0.0-beta2 (*)
| \--- com.fasterxml.jackson.core:jackson-databind:2.4.3
| +--- com.fasterxml.jackson.core:jackson-annotations:2.4.0
| \--- com.fasterxml.jackson.core:jackson-core:2.4.3
+--- com.fasterxml.jackson.datatype:jackson-datatype-joda:2.4.3
| +--- com.fasterxml.jackson.core:jackson-annotations:2.4.0
| +--- com.fasterxml.jackson.core:jackson-core:2.4.3
| +--- com.fasterxml.jackson.core:jackson-databind:2.4.3 (*)
| \--- joda-time:joda-time:2.2 -> 2.3
+--- com.box:json-schema-validator:2.2.10
| +--- joda-time:joda-time:2.3
Edit2:我删除了 joda-time:2.3 依赖项,没有结果。