记录feign调用的LocalDateTime字段转换失败的坑

  • Post author:
  • Post category:其他


关键的异常信息如下:

Caused by: java.time.format.DateTimeParseException: Text '2023-05-19 11:07:36' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {SecondOfMinute=36, NanoOfSecond=0, MilliOfSecond=0, HourOfAmPm=11, MicroOfSecond=0, MinuteOfHour=7},ISO,GMT+08:00 resolved to 2023-05-19 of type java.time.format.Parsed
	at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920) ~[?:1.8.0_333]
	at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855) ~[?:1.8.0_333]
	at java.time.LocalDateTime.parse(LocalDateTime.java:492) ~[?:1.8.0_333]
	at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer._fromString(LocalDateTimeDeserializer.java:177) ~[jackson-datatype-jsr310-2.13.4.jar:2.13.4]
	at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:81) ~[jackson-datatype-jsr310-2.13.4.jar:2.13.4]
	at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:40) ~[jackson-datatype-jsr310-2.13.4.jar:2.13.4]
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:314) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:177) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer._deserializeFromArray(CollectionDeserializer.java:359) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:244) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:28) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:314) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:177) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:314) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:177) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4730) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3730) ~[jackson-databind-2.14.2.jar:2.14.2]
	at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:380) ~[spring-web-5.3.23.jar:5.3.23]
	at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:343) ~[spring-web-5.3.23.jar:5.3.23]
	at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:105) ~[spring-web-5.3.23.jar:5.3.23]
	at org.springframework.cloud.openfeign.support.SpringDecoder.decode(SpringDecoder.java:75) ~[spring-cloud-openfeign-core-3.1.5.jar:3.1.5]
	at org.springframework.cloud.openfeign.support.ResponseEntityDecoder.decode(ResponseEntityDecoder.java:61) ~[spring-cloud-openfeign-core-3.1.5.jar:3.1.5]
	at feign.optionals.OptionalDecoder.decode(OptionalDecoder.java:36) ~[feign-core-11.10.jar:?]
	at feign.InvocationContext.proceed(InvocationContext.java:36) ~[feign-core-11.10.jar:?]
	... 64 more

偶尔异常信息是这样提示的:

Caused by: java.time.format.DateTimeParseException: Text '2023-05-19 11:07:36' could not be parsed at index 10
	at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) ~[?:1.8.0_333]
	at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) ~[?:1.8.0_333]
	at java.time.LocalDateTime.parse(LocalDateTime.java:492) ~[?:1.8.0_333]
	at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer._fromString(LocalDateTimeDeserializer.java:177) ~[jackson-datatype-jsr310-2.13.4.jar:2.13.4]
	at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:81) ~[jackson-datatype-jsr310-2.13.4.jar:2.13.4]
	at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:40) ~[jackson-datatype-jsr310-2.13.4.jar:2.13.4]
	at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:542) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeWithErrorWrapping(BeanDeserializer.java:564) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:439) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1405) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:352) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:185) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer._deserializeFromArray(CollectionDeserializer.java:359) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:244) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:28) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:314) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:177) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:314) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:177) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4730) ~[jackson-databind-2.14.2.jar:2.14.2]
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3730) ~[jackson-databind-2.14.2.jar:2.14.2]
	at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:380) ~[spring-web-5.3.23.jar:5.3.23]
	at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:343) ~[spring-web-5.3.23.jar:5.3.23]
	at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:105) ~[spring-web-5.3.23.jar:5.3.23]
	at org.springframework.cloud.openfeign.support.SpringDecoder.decode(SpringDecoder.java:75) ~[spring-cloud-openfeign-core-3.1.5.jar:3.1.5]
	at org.springframework.cloud.openfeign.support.ResponseEntityDecoder.decode(ResponseEntityDecoder.java:61) ~[spring-cloud-openfeign-core-3.1.5.jar:3.1.5]
	at feign.optionals.OptionalDecoder.decode(OptionalDecoder.java:36) ~[feign-core-11.10.jar:?]
	at feign.InvocationContext.proceed(InvocationContext.java:36) ~[feign-core-11.10.jar:?]
	... 64 more

同事的feign和bean是这样的:

@FeignClient(value = "xxxx")
public interface MallFeign {

    /**
     * 获取商城分页列表
     */
    @PostMapping("/mall/getMallPage")
    RetResult<PageInfo<Mall>> getMallPage(@RequestBody MallPageSearch search);
}


@Data
@AllArgsConstructor
@ApiModel("商城VO")
public class Mall {

    @ApiModelProperty(value = "商城ID")
    private Integer id;

    @ApiModelProperty(value = "商城名称")
    private String name;

    @ApiModelProperty(value = "备注")
    private String remarkInfo;

    @ApiModelProperty(value = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss", timezone = "GMT+8")
    private LocalDateTime createTime;
}

@。@表面看上去也没看出什么毛病。。。消费者这个bean和生产者用的是同一个

最终经多次测试,发现2个奇葩问题:

1、必须移除@AllArgsConstructor

2、日期格式,虽然和生产者用的都是一样的yyyy-MM-dd hh:mm:ss,但不改为yyyy-MM-dd HH:mm:ss就始终解析不了



版权声明:本文为shaoyelaozi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。