无法写入 JSON:找不到 class net.i2p.crypto.eddsa.math.ed25519.Ed25519LittleEndianEncoding 的序列化程序

Could not write JSON: No serializer found for class net.i2p.crypto.eddsa.math.ed25519.Ed25519LittleEndianEncoding

我尝试将 spring 网络服务器实现到 cordapp,但不断出现相同的序列化错误。

{
"timestamp": 1529999846743,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.http.converter.HttpMessageNotWritableException",
"message": "org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class net.i2p.crypto.eddsa.math.ed25519.Ed25519LittleEndianEncoding and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class net.i2p.crypto.eddsa.math.ed25519.Ed25519LittleEndianEncoding and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->java.util.Collections$SingletonMap[\"state\"]->java.util.Collections$SingletonMap[\"data\"]->java.util.LinkedHashMap[\"exitKeys\"]->java.util.LinkedHashSet[0]->net.i2p.crypto.eddsa.EdDSAPublicKey[\"params\"]->net.i2p.crypto.eddsa.spec.EdDSANamedCurveSpec[\"curve\"]->net.i2p.crypto.eddsa.math.Curve[\"field\"]->net.i2p.crypto.eddsa.math.Field[\"encoding\"])",
"path": "/api/obligation/cash"

}

基本上你正在尝试序列化 class net.i2p.crypto.eddsa.math.ed25519.Ed25519LittleEndianEncoding 的对象,正如你在 sources 中看到的那样,这个 class 没有字段,这会导致序列化失败。

您应该将此行添加到您的映射器配置中:

mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

然后看看输出是不是你想要的。

编辑:根据Spring Boot documentation,您可以通过添加

将此配置添加到默认映射器
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false

到您的 application.properties 文件。

当 Jackson 尝试将 PublicKey class 序列化为 JSON format 时,将引发此错误。 Corda 为某些内部 classes 提供 Jackson support,您必须在 Spring:

中注册该模块
  1. 在您的 spring 项目中添加 corda-jackson 依赖项。

    net.corda:corda-jackson:3.1-corda

  2. 为您的 spring 项目注册 Corda Jackson 支持 Module。您可以使用 java 配置如下:

@Bean public Module registerModule() { return JacksonSupport.INSTANCE.getCordaModule(); }

像这样在 @Configuration class 中创建一个 ObjectMapper :

@Configuration
class Plugin {

   @Bean
   fun registerModule(): ObjectMapper {
      return JacksonSupport.createNonRpcMapper()
   }
}

一切顺利!

请在 bean 的 class 级别使用它:

@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler"})

这应该是因为缺少 JacksonSupport 或 corda-jackson 配置不当。

以我为例: 我已经有:

implementation("com.fasterxml.jackson.module:jackson-module-kotlin")

但错误仍然存​​在。

  1. 我在 build.gradle.kts 中添加了 corda-jackson。
implementation( "net.corda:corda-jackson:4.3")
  1. 我在 @SpringBootApplication 文件中添加了以下 Bean JacksonSupport。
@Bean
  @Primary
  fun objectMapper(builder: Jackson2ObjectMapperBuilder): ObjectMapper? {
      return JacksonSupport.createNonRpcMapper().findAndRegisterModules()
  }

错误仍然存​​在。

  1. 然后,我将以下行添加到 build.gradle.kts
extra["jackson.version"] = "2.10.2"
  1. 终于成功了:)