我有Micronaut Http Client
从Kotlin
另一个微服务加载数据的方法。此微服务的日期格式有时无效。
示例:正常模式是dd-MM-yyyy
,但是当没有日期时,微服务将其返回为" - - "
。所以,当日期为" - - "
. 我已经使用过@JsonDeserialize(using = "CustomDateDeserialize")
,但是当不满足模式时,我无法不反序列化日期字段。
我的客户
@Get(value = ..., produces = [MediaType.APPLICATION_JSON])
@Headers(...)
fun findParcels(...) : HttpResponse<MyDTO>
我的DTO
@KotlinBuilder
data class ParcelsDTO(
@JsonProperty("property1")
val property1: String
@JsonProperty("property2")
val property2: Long
@JsonProperty("property3")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
val property3: LocalDate
@JsonProperty("property4")
val property4: BigDecimal
@JsonProperty("property5")
@JsonDeserialize(using = CustomLocalDateDeserializer::class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
val property5: LocalDate
)
MyCustomDeserializer
class CustomLocalDateDeserializer protected constructor() : StdDeserializer<LocalDate?>(LocalDate::class.java) {
@Throws(IOException::class)
override fun deserialize(parser: JsonParser, context: DeserializationContext?): LocalDate {
val node: JsonNode = parser.codec.readTree(parser)
val itemName = node.get("property5").asText()
if(!GenericValidator.isDate(itemName, "dd-MM-yyyy", true)){
//here was to set to not serialize the node
}
return LocalDateDeserializer.INSTANCE.deserialize(parser, context)
}
}
源 Json
{
[
{
"property1": "string",
"property2": 123,
"property3": "01-01-2021",
"property4": 1.20,
"property5": " - - ",
},
{
"property1": "string2",
"property2": 123,
"property3": "01-01-2021",
"property4": 1.20,
"property5": "01-01-2022",
},
]
}