Feign jackson dateTime JsonMappingException

浪尽此生 提交于 2019-12-25 02:46:34

问题


I've got two services that are communicating with openfeign (github.openfeign:10.2.0). I'm forcing problem with deserializing datatime that is sent as JSON. Here is my configuration:

@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
)

Here is exception:

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"])

Here is Json:

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

Dependencies:

    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"

回答1:


When we take a look how JacksonEncoder and JacksonDecoder are implemented we will notice they create new ObjectMapper in constructor:

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

Above constructor is invoked by default with emptyList(). So, we need to provide all modules we want to use manually.

To get the most from Java 8 and Jackson register all 3 modules from jackson-modules-java8:

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

EDIT
Ther is a constructor which allow to use ObjectMapper instance. You can create new instance or inject from your container:

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)


来源:https://stackoverflow.com/questions/55028766/feign-jackson-datetime-jsonmappingexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!