1

我有Micronaut Http ClientKotlin另一个微服务加载数据的方法。此微服务的日期格式有时无效。

示例:正常模式是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",
      },   
   ]
}
4

1 回答 1

0

您的自定义解串器所做的比它必须做的更多。您只需要处理日期值。看看它应该是什么样子:

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.BeanProperty
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer
import java.io.IOException
import java.time.LocalDate
import java.time.format.DateTimeFormatter

class CustomLocalDateDeserializer : LocalDateDeserializer(DateTimeFormatter.ofPattern("dd-MM-yyyy")) {

    @Throws(IOException::class)
    override fun deserialize(parser: JsonParser, context: DeserializationContext?): LocalDate {
        val value: String? = parser.text

        if (value == null || value == " - - ") {
            // skip deserialisation and return default date
            return LocalDate.of(2000, 1, 1)
        }
        return super.deserialize(parser, context)
    }

    override fun createContextual(ctxt: DeserializationContext?, property: BeanProperty?): JsonDeserializer<*> {
        return this
    }
}

您可以按如下方式注册:

@JsonProperty("property5")
@JsonDeserialize(using = CustomLocalDateDeserializer::class)
val property5: LocalDate
于 2021-06-04T20:22:02.507 回答