简体   繁体   English

无法将“java.lang.String”类型的值转换为所需类型“java.util.Date”

[英]Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'

I have 2 microservices that are communicating via feign.我有 2 个通过 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:另一个微服务正在使用这个 feign 类来调用这个方法:

@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) ==> 第二个微服务正在另一个类中调用这个方法,并给它一个从数据库中检索的日期(格式: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';无法将“java.lang.String”类型的值转换为所需类型“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';嵌套异常是 org.springframework.core.convert.ConversionFailedException:无法从类型 [java.lang.String] 转换为类型 [@org.springframework.web.bind.annotation.PathVariable @org.springframework.format.annotation.DateTimeFormat java.util.Date] 值 '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\\""嵌套异常是 java.lang.IllegalArgumentException: Invalid format: \\"2019-03-29 08:38:08.0\\" is malformed at \\" 08:38:08.0\\""

Feign client doesn't support @DateTimeFormat annotation as per issue Feign client doesn't serialize java.time.LocalDate's correctly #104 . Feign 客户端不支持@DateTimeFormat注释,因为Feign 客户端没有正确序列化 java.time.LocalDate 的问题 #104 The @DateTimeFormat annotation is used by Spring MVC, not by Spring Cloud OpenFeign. @DateTimeFormat注解由 Spring MVC 使用,而不是由 Spring Cloud OpenFeign 使用。

There are few ways to fix eg force ISO dates for all Feign calls or register additional Spring converters .有几种方法可以修复例如强制所有 Feign 调用的 ISO 日期注册额外的 Spring 转换器 It will require custom code that depends on your use-case eg you might want to replace old Date with ZonedDateTime .它将需要取决于您的用例的自定义代码,例如您可能想用ZonedDateTime替换旧的Date

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将 java.lang.String 类型的属性值转换为所需类型 java.util.Date - Failed to convert property value of type java.lang.String to required type java.util.Date 无法将类型'java.lang.String'的属性值转换为必需类型'java.util.Date' - Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' 无法将 java.lang.String 类型的值转换为所需的 java.util.Date 类型 - Failed to convert value of type java.lang.String to required type java.util.Date 如何将“java.lang.String”类型的值转换为所需的“java.util.Date”类型? - How to convert value of type 'java.lang.String' to required type 'java.util.Date'? 使用@DateTimeFormat(pattern =“ HH:mm”)无法将类型[java.lang.String]的属性值转换为所需的类型[java.util.Date] - Failed to convert property value of type [java.lang.String] to required type [java.util.Date] with @DateTimeFormat(pattern = “HH:mm”) 错误:无法将类型java.lang.String的属性值转换为属性dob的必需类型java.util.Date? - Error: Failed to convert property value of type java.lang.String to required type java.util.Date for property dob? 无法将属性“ dateOfBirth”的类型“ java.lang.String”的属性值转换为所需的类型“ java.util.Date”; - Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'dateOfBirth'; 发送 GET 请求时无法将“java.lang.String”类型的值转换为所需类型“java.util.Date” - Failed to convert value of type 'java.lang.String' to required type 'java.util.Date' when sending GET request 无法将属性fromDate的java.lang.String类型的属性值转换为所需的java.util.Date类型; - Failed to convert property value of type java.lang.String to required type java.util.Date for property fromDate; 无法将类型“java.lang.String”的属性值转换为属性“日期”所需的类型“java.util.Date”:它不完全是 10 个字符长 - Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date': it is not exactly10characters long
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM