当前位置: 首页 > 知识库问答 >
问题:

Jackson无法反序列化Spring的org.springframework.data.domain.sort

慕金林
2023-03-14

问题:假客户机对返回的Spring boot Rest API进行API调用时,无法反序列化该页的sort属性。

  • Spring Boot:2.3.3.发布
  • 春云假:2.2.5.发布

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)

共有1个答案

从智志
2023-03-14

如果您使用自动配置的Feign客户端,您可以通过打开相应的配置属性来跟踪Spring Cloud OpenFeign文档:

您可以考虑启用Jackson模块来支持org.springframework.data.domain.pageorg.springframework.data.domain.sort解码。

feign.autoconfiguration.jackson.enabled=true
 类似资料:
  • 这是错误20-01-22 Wed14:17:56.475WARN MappingJackson2HttpMessageConzer无法评估Jackson反序列化类型[[简单类型,类lk.andunaechomedia.models.设备]]:com.fasterxml.jackson.databind.JsonMappingExc0019:多个返回引用属性与名称'user-运动'20-01-22周

  • 问题内容: 我正在尝试使用Jackson将json数据转换为POJO对象。这是MainActivity和我的POJO类代码。我基本上收到了JsonMappingException错误。我还附上了整个日志。 MainActivity.java: Entries.java(这是POJO) 现在,我的日志中出现以下错误。因此,我无法继续工作。这是日志: 问题答案: 在 条目* 和 电话中 删除构造函数

  • 代码试图采用java.awt.color类使用jackson对象映射器对其进行序列化。获取生成的json字符串并将其反序列化回java.awt.color类。但是在进行反序列化时会出现以下错误。 线程"main"com.fasterxml.jackson.databind.JsonMappingException中的异常:找不到适合类型[简单类型,类java.awt.颜色]的构造函数:无法从JSO

  • 我正在阅读Facebook的洞察,并试图让Jackson将JSON映射到Object。如果所有的数据都不是空的,我就会让它正常工作。但是我在尝试反序列化键值的空数组时遇到了问题。即使尝试了这篇文章:如何防止Map内部的null值和bean内部的null字段通过Jackson序列化,也没有解决问题:( 这是JSON: 我的代码段如下: 全堆栈跟踪:

  • 我试图在电影和用户之间建立多对多的关系。当我保存电影时,我会收到这个错误: 2017-12-01 16:12:43.351警告17328---[nio-8090-exec-5]。c、 j.MappingJackson2HttpMessageConverter:未能评估类型[[simple type,class com.movieseat.models.Movie]]:java的Jackson反序列