java - 如何从 FeignClient 端点返回 LocalDateTime?

标签 java spring rest spring-cloud-feign localdate

这是我的 FeignClient:

@FeignClient(name="${mongo.service.id}", url="${mongo.service.url}", configuration = FeignConfig.class)
public interface MongoAtmResetDataInterface {
    String requestMappingPrefix = "/api/atmResetData";

    @GetMapping(path = requestMappingPrefix + "/brinksDateTime")
    LocalDateTime fetchLastBrinksDateTime();
}

这是对 feign 端点的调用:

private String fetchLastBrinksTime() {
    return mongoAtmResetDataInterface.fetchLastBrinksDateTime()
       .toLocalDate()
       .format(DateTimeFormatter.ofPattern(DATE_FORMAT));
}

我收到以下异常:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 
Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): 
no String-argument constructor/factory method to deserialize from String value ('10-12-2019T14:01:39')

我的 SpringMvcConfig 类中有一个 LocalDateTime 转换器,而我的 FeignConfig 类中有一个合约。 谁能帮忙 - 我错过了什么?

最佳答案

Spring MVC 使用反序列化将生成一个数组。但Feign用ArrayList调用对象方法。所以你不能反序列化LocalDate。

所以你可以在pom.xml中添加这个设置

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>

并将其添加到反序列化模型。 (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule,com.fasterxml.jackson.datatype.jsr310.JSR310Module)

@Bean
public ObjectMapper serializingObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    objectMapper.registerModule(new JavaTimeModule());
    return objectMapper;
}

希望可以帮到你。

关于java - 如何从 FeignClient 端点返回 LocalDateTime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60429964/

相关文章:

php - 在 PHP 中调用 REST API

rest - 如何使用 ExtJS 3.1.0 获取 FormPanel 以发布 JSON?

java - 当我使用 findViewById 时,将 R.id.[value] 中的 int 作为类构造函数参数传递给 NPE

java - $avg 未返回平均值

java - 在 Maven/Spring Boot 项目中运行测试时出现 MojoFailureException (NoClassDefFounderror)

java - 如何在 log4j.properties 文件中设置占位符的值?

java - SPRING:无法使用休息模板在发布请求中传递参数

java - HttpRequestHandlerServlet 中的 Spring 请求范围不起作用

java - SSL 握手警报 : unrecognized_name error since upgrade to Java 1. 7.0

java - JPA 实体在 Ubuntu 上得到更新但在 Windows 上抛出乐观锁定异常