openFeign传递Date对象

直接传递Date对象会出现异常Request","trace":"org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type ... (7684 bytes)],或者是时区慢了14小时,解决方案是在消费者端把Date转为字符串传递,然后在提供者端把字符串转为Date类型,代码如下
consumer消费者端

@GetMapping("/testTime")
public String testTime(){
    Date date = new Date();
    System.out.println(date); //Sun Nov 13 16:19:05 GMT+08:00 2022
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String formatDate = sdf.format(date);  // 转为 2022-11-13 16:19:05字符串进行传递
    String result = userOrderFeign.time(formatDate);
    return result;
}
//feign接口
//单独传递一个时间参数
@GetMapping("/time")
public String time(@RequestParam String date);

provider提供者端

//单独传递一个时间参数
@GetMapping("/time")
public String time(@RequestParam String date){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Date datetime = null;
    try {
        datetime = sdf.parse(date); //把字符串转为Date类型 即2022-11-13 16:19:05转为Sun Nov 13 16:19:05 GMT+08:00 2022 
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return "ok";
}

以上是openfeign传递Date对象的解决方案。

posted @ 2022-11-13 16:33  HainChen  阅读(495)  评论(0编辑  收藏  举报