Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-cloud-feign Client and @RequestParam with Date type

This time I was working with Declarative REST Client, Feign in some Spring Boot App.

What I wanted to achieve is to call one of my REST API's, which looks like:

@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) {

As You can see, the API accepts calls with from and to Date params formatted like (yyyy-MM-dd)

In order to call that API, I've prepared following piece of @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);
}

Generally, almost copy-paste. And now somewhere in my boot App, I use that:

DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
ResponseEntity response = inboundACustomersClient.customersLastVisit(formatter.parse(formatter.format(from)),
        formatter.parse(formatter.format(to)));

And, what I get back is that

nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value 'Sun May 03 00:00:00 CEST 2015';

nested exception is java.lang.IllegalArgumentException: Unable to parse 'Sun May 03 00:00:00 CEST 2015'

So, the question is, what am I doing wrong with request, that it doesn't parse to "date-only" format before sending to my API? Or maybe it is a pure Feign lib problem?

like image 290
patrykos91 Avatar asked Feb 02 '16 15:02

patrykos91


2 Answers

You should create and register a feign formatter to customize the date format

@Component
public class DateFormatter implements Formatter<Date> {

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

    @Override
    public Date parse(String text, Locale locale) throws ParseException {
        return formatter.parse(text);
    }

    @Override
    public String print(Date date, Locale locale) {
        return formatter.format(date);
    }
}


@Configuration
public class FeignFormatterRegister implements FeignFormatterRegistrar {

    @Override
    public void registerFormatters(FormatterRegistry registry) {
        registry.addFormatter(new DateFormatter());
    }
}
like image 193
Rafael Zeffa Avatar answered Sep 29 '22 13:09

Rafael Zeffa


The feign client now (Dec 2020) works fine using the syntax in the original question above and a java.time.LocalDate as the parameter. That is, you can use:

  @FeignClient(name = "YOUR-SERVICE")
  interface ClientSpec {
    @RequestMapping(value = "/api/something", method = RequestMethod.GET)
    String doSomething(
        @RequestParam("startDate") 
        @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) 
        LocalDate startDate);
  }
like image 40
Andy Brown Avatar answered Sep 29 '22 13:09

Andy Brown