一、报错信息

A服务调用B服务,由于B服务接口实体类中有LocalDateTime字段,导致A服务调用时解析报错,单独调用B服务接口是正常返回数据。A服务报错新消息如下:

feign.codec.DecodeException: Type definition error: [simple type, class java.time.LocalDateTime]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2020-05-20T16:00:32')
Caused by: org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class java.time.LocalDateTime]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2020-05-20T16:00:32')
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Ca

二、解决方案

1、引入jar包

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.9.9</version>
</dependency>
<dependency>
     <groupId>com.fasterxml.jackson.core</groupId>
     <artifactId>jackson-databind</artifactId>
     <version>2.9.5</version>
</dependency>

2、服务提供方(B服务)接口实体类需要增加注解


import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.format.annotation.DateTimeFormat;

@Data
public class ProMktPlan implements Serializable {

@DateTimeFormat(
        pattern = "yyyy-MM-dd HH:mm:ss"
    )
    @JsonFormat(
        shape = Shape.STRING,
        pattern = "yyyy-MM-dd HH:mm:ss"
    )
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime contractDate;

}

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐