假装杰克逊日期时间 JsonMappingException

Feign jackson dateTime JsonMappingException

我有两个服务正在与 openfeign (github.openfeign:10.2.0) 通信。我在反序列化作为 JSON 发送的数据时间时遇到了问题。 这是我的配置:

@Configuration
class JacksonConfig : WebMvcConfigurerAdapter() {
    override fun extendMessageConverters(converters: List<HttpMessageConverter<*>>?) {
        for (converter in converters!!) {
            if (converter is MappingJackson2HttpMessageConverter) {
                val objectMapper = converter.objectMapper
                objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                objectMapper.disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
                break
            }
        }
    }
}
    private val client = Feign.builder()
        .encoder(JacksonEncoder())
        .decoder(JacksonDecoder())
        .target(Client::class.java, host)

@JsonIgnoreProperties(ignoreUnknown = true)
data class XYZ(
  var id: Int? = null,
  val X: Int? = null,
  val Y: Int? = null,
  @Enumerated(EnumType.STRING)
  val z: Type? = null,
  @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
  val createdOn: LocalDateTime? = null
)

这里有例外:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is feign.FeignException: Can not construct instance of java.time.LocalDateTime: no String-argument constructor/factory method to deserialize from String value ('2019-03-06T16:50:53')
  at [Source: java.io.BufferedReader@3c14b300; line: 1, column: 77] (through reference chain: java.util.HashSet[0]->XYZ["createdOn"]) reading GET http://localhost:8080/path] with root cause 
  com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.LocalDateTime: no String-argument constructor/factory method to deserialize from String value ('2019-03-06T16:50:53')
  at [Source: java.io.BufferedReader@3c14b300; line: 1, column: 77] (through reference chain: java.util.HashSet[0]->XYZ["createdOn"])

这里是Json:

[
  {
    "id": 2,
    "X": 1,
    "U": 1,
    "Z": "ABC",
    "createdOn": "2019-03-06T16:50:53"
  }
]

依赖关系:

    compile group: 'io.github.openfeign', name: 'feign-core', version: '10.2.0'
    compile group: 'io.github.openfeign', name: 'feign-jackson', version: '10.2.0'
    compile "com.fasterxml.jackson.datatype:jackson-datatype-jdk:$jackson_version"
    compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version"
    compile "com.fasterxml.jackson.module:jackson-module-kotlin:$jackson_config"

当我们看一下 JacksonEncoder and JacksonDecoder 是如何实现的时,我们会注意到它们在构造函数中创建了新的 ObjectMapper

public JacksonDecoder(Iterable<Module> modules) {
  this(new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
      .registerModules(modules));
}

以上构造函数默认使用 emptyList() 调用。所以,我们需要手动提供我们想要使用的所有模块。

为了充分利用 Java 8Jackson 注册来自 jackson-modules-java8 的所有 3 个模块:

private val client = Feign.builder()
    .encoder(JacksonEncoder(Arrays.asList(JavaTimeModule(), ...)))
    .decoder(JacksonDecoder(Arrays.asList(JavaTimeModule(), ...)))
    .target(Client::class.java, host)

编辑
还有一个允许使用 ObjectMapper 实例的构造函数。您可以创建新实例或从容器中注入:

val mapper = ObjectMapper()
mapper.registerModule(ParameterNamesModule())
   .registerModule(Jdk8Module())
   .registerModule(JavaTimeModule())
// other configuration

val client = Feign.builder()
    .encoder(JacksonEncoder(mapper))
    .decoder(JacksonDecoder(mapper))
    .target(Client::class.java, host)