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"