spring cloud中微服务调用出现 Can not parse date while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS问题

    最近在使用spring cloud开发微服务,在测试A服务调用B服务过程中出现了下面的异常:

    

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize value of type java.util.Date from String "2018-04-04 14:48:30": not a valid representation (error: Failed to parse Date value '2018-04-04 14:48:30': Can not parse date "2018-04-04 14:48:30": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2018-04-04 14:48:30": not a valid representation (error: Failed to parse Date value '2018-04-04 14:48:30': Can not parse date "2018-04-04 14:48:30": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null))
 at [Source: java.io.PushbackInputStream@4d9b0ce9; line: 1, column: 200] (through reference chain: com.zsagepay.user.dto.UserDto["createDate"])

    当时的场景:

        A调B服务的查询用户的接口,返回用户数据(userDto),A修改 返回用户数据中的用户姓名和头像,身份证等等信息后,并掉B的更新用户接口,更新用户信息。userDto中有创建时间、更新时间等参数。


    出现异常原因:

        B返回给A的用户数据中创建时间和更新时间的日期格式为: 'yyyy-MM-dd HH:mm:ss’,A在调用B的时候,请求数据格式为: 'yyyy-MM-dd HH:mm:ss',但是B在接收到数据的时候,需要通过jackson把数据转化成userDto对象。jackson转化的时候,默认的时间格式是 'yyyy-MM-dd'T'HH:mm:ss.SSS’,所以就会出现上面的异常。

    

    解决方法:

    在userDto中的日期格式上增加下面注解就可以解决:

    

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")

如:创建时间字段:

@ApiModelProperty(value = "创建时间 ;不需要传")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createDate;

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值