I have 2 microservices that are communicating via feign. One micro-service is exposing a method that the other is calling. The method takes a date as an argument.
This is the code of the method :
@Override
public String verifyWorkingDaysByAccesstimeAndAttendanceDate(@PathVariable int accessTimeId,
@PathVariable("attendanceDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date attendanceDate) {
AccessTime accessTime = accessTimeService.findByIDService(AccessTime.class, accessTimeId);
String day = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(attendanceDate).toUpperCase();
String status = "NOT_VERIFIED";
if (workingDayservice.findWorkingDaysByAccesstimeAndDate(accessTime, attendanceDate).toString().contains(day)) {
status = "VERIFIED";
}
return status;
}
==> as you can see it is taking date and verifying its existance in the database.
the other microservice is using this feign class in order to call this method:
@FeignClient(name = "access-control-micro-services-access-time")
public interface AccessTimeClient {
@RequestMapping(value = "/api/v${webService.currentVersion}/workingDay/accessTime/{accessTimeId}/{attendanceDate}", method = RequestMethod.GET)
String verifyWorkingDaysByAccesstimeAndAttendanceDate(@PathVariable("accessTimeId") int accessTimeId,
@PathVariable("attendanceDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date attendanceDate);
==> the second microservice is calling this method in another class and is giving it a date that it is retreiving from the database (format: 2019-03-29 08:38:08)
My code is launching this exception :
Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.PathVariable @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '2019-03-29 08:38:08.0'; nested exception is java.lang.IllegalArgumentException: Invalid format: \"2019-03-29 08:38:08.0\" is malformed at \" 08:38:08.0\""
Feign client doesn't support
@DateTimeFormatannotation as per issue Feign client doesn't serialize java.time.LocalDate's correctly #104. The@DateTimeFormatannotation is used by Spring MVC, not by Spring Cloud OpenFeign.There are few ways to fix e.g. force ISO dates for all Feign calls or register additional Spring converters. It will require custom code that depends on your use-case e.g. you might want to replace old
DatewithZonedDateTime.