【发布时间】:2016-05-11 12:42:19
【问题描述】:
这次我在使用声明式 REST 客户端,在一些 Spring Boot 应用程序中进行 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 解析“2015 年 5 月 3 日星期日 00:00:00 CEST”
所以,问题是,我对请求做错了什么,它在发送到我的 API 之前没有解析为“仅限日期”格式?或者可能是纯粹的 Feign lib 问题?
【问题讨论】:
标签: java spring rest jackson netflix-feign