可以把get请求改成post请求(其实本地服务没有问题,但是部署到服务器上就出现了这个问题)
好像没有没有解决实际问题。
参考:
Feign调用GET方法,入参POJO对象有LocalDateTime属性遇到的问题和解决办法 - 掘金
后续
— — — — — — — —
后面又报错了,还是那个类型转换异常的问题。
报错信息:
[500 ] during [POST] to [http://xyfintec-etsp-order/orderInfo/check/suitable/student?courseId=1568059030997725186&startTime=22-9-9%20%E4%B8%8B%E5%8D%881%3A30] [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%20%E4%B8%8B%E5%8D%881%3A30,在服务调用的时候被encode了,导致后面其他服务接收的时候,转LocalDateTime,转换异常。
解决办法:
1、在feign远程调用的时候,先将localDateTime格式化成字符串,然后传输;
2、等到达目标服务之后,在将其转换成LocalDateTime类型。
feign接口:
@FeignClient(value = ServiceNameConstants.SERVICE_ORDER, contextId = "orderInfo")
public interface RemoteOrderInfoService {
/**
* 远程调用-校验满足上课条件的学生
* @param studentInfos
* @param courseId
* @param startTime
* @return
*/
@PostMapping("/orderInfo/check/suitable/student")
WebReturnResultModel<List<RemoteStudentInfoDTO>> checkSuitableStudent(@RequestBody List<RemoteStudentInfoDTO> studentInfos,
@RequestParam("courseId") String courseId,
@RequestParam(value = "startTime", required = false) String startTime);
...
}
远程服务接口:
/**
* 远程调用-校验满足上课条件的学生
*/
@PostMapping("/check/suitable/student")
public WebReturnResultModel<List<RemoteStudentInfoDTO>> checkSuitableStudent(@RequestBody List<RemoteStudentInfoDTO> studentInfos,
@RequestParam("courseId") String courseId,
@RequestParam(value = "startTime", required = false) String startTime) {
...
}
实际调用:
实际调用时转成字符串
// Feign在远程调用是,使用LocalDate需要转换成字符串
String startTime = LocalDateTimeUtil.format(courseInfo.getStartTime(), DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));
到达目标后:
到达目标后再将其转换成LocalDateTime
// Feign在远程调用是,使用LocalDate需要转换成字符串
LocalDateTime parseStartTime = LocalDateTimeUtil.parse(startTime, DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));
补充:这里用到了hutool的工具类(感觉还不错)
依赖:
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.5</version>
</dependency>