问题:一个 feign 客户端,对返回的 Spring Boot Rest API 进行 API 调用,该 APIPage<T>
无法反序列sort
化该页面的属性。
- Spring Boot:2.3.3.Release
- Spring Cloud Feign:2.2.5.RELEASE
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造实例
org.springframework.data.domain.Sort
(没有创建者,如默认构造函数,存在):无法从 [来源:(BufferedReader)的对象值(没有基于委托或基于属性的创建者)反序列化;行:1,列:238](通过引用链:org.springframework.cloud.openfeign.support.PageJacksonModule$SimplePageImpl["sort"])
不知道为什么注册PageJacksonModule
似乎不支持这一点。
给定一个手动配置的 Feign 客户端:
public class TelematicsConfig {
private String host;
ObjectMapper provideObjectMapper() {
return new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setPropertyNamingStrategy(SnakeCaseStrategy.SNAKE_CASE)
.registerModule(new PageJacksonModule());
}
@Bean
TelematicsClient provideTelematicsClient() {
return Feign.builder()
.client(new OkHttpClient())
.encoder(new JacksonEncoder(provideObjectMapper()))
.decoder(new JacksonDecoder(provideObjectMapper()))
.logger(new Slf4jLogger(TelematicsClient.class))
.logLevel(Logger.Level.FULL)
.target(TelematicsClient.class, host);
}
}
客户本身:
public interface TelematicsClient {
@RequestLine("GET /api/v1/telematics/devices")
Page<TelematicsDevice> getDevices();
}
调用它时,我得到:
2020-09-16 12:38:49.707 ERROR 96244 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is feign.FeignException: Cannot construct instance of `org.springframework.data.domain.Sort` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (BufferedReader); line: 1, column: 238] (through reference chain: org.springframework.cloud.openfeign.support.PageJacksonModule$SimplePageImpl["sort"]) reading GET http://localhost:8081/api/v1/telematics/devices] with root cause
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.data.domain.Sort` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (BufferedReader); line: 1, column: 238] (through reference chain: org.springframework.cloud.openfeign.support.PageJacksonModule$SimplePageImpl["sort"])
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1611)
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1077)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1320)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:331)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:164)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:542)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeWithErrorWrapping(BeanDeserializer.java:535)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:419)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1310)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:331)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:164)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4482)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3463)
at feign.jackson.JacksonDecoder.decode(JacksonDecoder.java:61)
任何关于为什么这不起作用的见解将不胜感激。
编辑:以下课程似乎暗示支持排序,不是吗?