这次我在一些 Spring Boot 应用程序中使用声明式 REST 客户端 Feign。
我想要实现的是调用我的 REST API 之一,它看起来像:
@RequestMapping(value = "/customerslastvisit", method = RequestMethod.GET)
public ResponseEntity customersLastVisit(
@RequestParam(value = "from", required = true) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date from,
@RequestParam(value = "to", required = true) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date to) {
如您所见,API 接受使用 from 和 to 日期参数的调用,格式如下(yyyy-MM-dd)
为了调用该 API,我准备了以下部分@FeignClient
:
@FeignClient("MIIA-A")
public interface InboundACustomersClient {
@RequestMapping(method = RequestMethod.GET, value = "/customerslastvisit")
ResponseEntity customersLastVisit(
@RequestParam(value = "from", required = true) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date from,
@RequestParam(value = "to", required = true) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date to);
}
一般来说,几乎是复制粘贴。现在在我的启动应用程序的某个地方,我使用它:
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
ResponseEntity response = inboundACustomersClient.customersLastVisit(formatter.parse(formatter.format(from)),
formatter.parse(formatter.format(to)));
而且,我得到的是
嵌套异常是 org.springframework.core.convert.ConversionFailedException: 无法从类型 [java.lang.String] 转换为类型 [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.util.Date] 的值'Sun May 03 00:00:00 CEST 2015';
嵌套异常是 java.lang.IllegalArgumentException: Unable to parse 'Sun May 03 00:00:00 CEST 2015'
所以,问题是,我对请求做错了什么,它在发送到我的 API 之前没有解析为“仅限日期”格式?或者它可能是一个纯粹的 Feign lib 问题?