简体   繁体   English

如何从 FeignClient 端点返回 LocalDateTime?

[英]How to return LocalDateTime from FeignClient endpoint?

This is my FeignClient:这是我的 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();
}

This is the call to the feign endpoint:这是对 feign 端点的调用:

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

I get the following exception:我收到以下异常:

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')

I do have a LocalDateTime converter in my SpringMvcConfig class & a contract in my FeignConfig class.我的 SpringMvcConfig 类中有一个 LocalDateTime 转换器,而 FeignConfig 类中有一个合约。 Can anyone help- what am I missing?任何人都可以帮忙-我错过了什么?

Spring MVC using deserialize will make to an Array.使用反序列化的 Spring MVC 将生成一个数组。 But Feign call the object method with ArrayList.但是Feign用ArrayList调用对象方法。 so you can not deserialize LocalDate.所以你不能反序列化 LocalDate。

So you can add this setting in pom.xml所以你可以在 pom.xml 中添加这个设置

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

And add this to deserialize model.并将其添加到反序列化模型中。 (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule, com.fasterxml.jackson.datatype.jsr310.JSR310Module) (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;
}

wish can help you.希望能帮到你。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM