• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Feign远程调用,传参为LocalDateTime类型报错问题

武飞扬头像
是你啊小苦瓜
帮助1

        可以把get请求改成post请求(其实本地服务没有问题,但是部署到服务器上就出现了这个问题)

        好像没有没有解决实际问题。

参考:

后续

— — — — — — — —

后面又报错了,还是那个类型转换异常的问题。

报错信息:

[500 ] during [POST] to [http://xyfintec-etsp-order/orderInfo/check/suitable/student?courseId=1568059030997725186&startTime=22-9-9 下午1:30] [RemoteOrderInfoService#checkSuitableStudent(List,String,LocalDateTime)]: [{"code":500,"msg":"Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed... (480 bytes)]

其实大概信息也能明白,就是说startTime=22-9-9 下午1:30,在服务调用的时候被encode了,导致后面其他服务接收的时候,转LocalDateTime,转换异常。

解决办法:

        1、在feign远程调用的时候,先将localDateTime格式化成字符串,然后传输;

        2、等到达目标服务之后,在将其转换成LocalDateTime类型。

feign接口:

  1.  
    @FeignClient(value = ServiceNameConstants.SERVICE_ORDER, contextId = "orderInfo")
  2.  
    public interface RemoteOrderInfoService {
  3.  
     
  4.  
     
  5.  
    /**
  6.  
    * 远程调用-校验满足上课条件的学生
  7.  
    * @param studentInfos
  8.  
    * @param courseId
  9.  
    * @param startTime
  10.  
    * @return
  11.  
    */
  12.  
    @PostMapping("/orderInfo/check/suitable/student")
  13.  
    WebReturnResultModel<List<RemoteStudentInfoDTO>> checkSuitableStudent(@RequestBody List<RemoteStudentInfoDTO> studentInfos,
  14.  
    @RequestParam("courseId") String courseId,
  15.  
    @RequestParam(value = "startTime", required = false) String startTime);
  16.  
    ...
  17.  
    }
学新通

远程服务接口:

  1.  
    /**
  2.  
    * 远程调用-校验满足上课条件的学生
  3.  
    */
  4.  
    @PostMapping("/check/suitable/student")
  5.  
    public WebReturnResultModel<List<RemoteStudentInfoDTO>> checkSuitableStudent(@RequestBody List<RemoteStudentInfoDTO> studentInfos,
  6.  
    @RequestParam("courseId") String courseId,
  7.  
    @RequestParam(value = "startTime", required = false) String startTime) {
  8.  
    ...
  9.  
    }

实际调用:

        实际调用时转成字符串

  1.  
    // Feign在远程调用是,使用LocalDate需要转换成字符串
  2.  
    String startTime = LocalDateTimeUtil.format(courseInfo.getStartTime(), DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));

到达目标后:

        到达目标后再将其转换成LocalDateTime

  1.  
    // Feign在远程调用是,使用LocalDate需要转换成字符串
  2.  
    LocalDateTime parseStartTime = LocalDateTimeUtil.parse(startTime, DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));

补充:这里用到了hutool的工具类(感觉还不错)

依赖:

  1.  
    <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
  2.  
    <dependency>
  3.  
    <groupId>cn.hutool</groupId>
  4.  
    <artifactId>hutool-all</artifactId>
  5.  
    <version>5.8.5</version>
  6.  
    </dependency>

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgjjjci
系列文章
更多 icon
同类精品
更多 icon
继续加载