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"
When we take a look how
JacksonEncoder
andJacksonDecoder
are implemented we will notice they create newObjectMapper
in constructor: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
andJackson
register all 3 modules from jackson-modules-java8:EDIT
Ther is a constructor which allow to use
ObjectMapper
instance. You can create new instance or inject from your container: