这是我的 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/