首页 > 解决方案 > Corda 查询抛出“com.fasterxml.jackson.databind.JsonMappingException:对象不是声明类的实例”

问题描述

我正在使用 example-cordapp 项目作为参考开发 cordapp。我已经能够向分类帐提交事务,甚至在节点上运行查询以查看它是否真的存在。但是,当我尝试从我的 Spring Boot 应用程序运行查询时,我收到了这个错误。

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request 
processing failed; nested exception is 
org.springframework.http.converter.HttpMessageConversionException: JSON mapping problem: 
java.util.Collections$UnmodifiableRandomAccessList[0]->net.corda.core.contracts.StateAndRef["state"]- 
>net.corda.core.contracts.TransactionState["data"]- 
>com.mypackage.states.MyState["party"]; nested exception is 
com.fasterxml.jackson.databind.JsonMappingException: object is not an instance of declaring class 
(through reference chain: java.util.Collections$UnmodifiableRandomAccessList[0]- 
>net.corda.core.contracts.StateAndRef["state"]->net.corda.core.contracts.TransactionState["data"]- 
>com.mypackage.states.MyState["party"])] with root cause

java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_251]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_251]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~ 
[na:1.8.0_251]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_251]

这是请求代码

 @GetMapping(value = [ "/api/v1/states" ], produces = [MediaType.APPLICATION_JSON_VALUE])
fun getMyIOUs(): ResponseEntity<List<StateAndRef<MyState>>>  {
    val myStates = proxy.vaultQueryBy<MyState>().states
    return ResponseEntity.ok(myStates)
}

这是州代码

@BelongsToContract(com.sentinel.contract.SharingInformationContract::class)
class SharingInformationState(
    val party: Party,
    val dataOwnerId: Long,
    val dataBuyerId: Long,
    override val linearId: UniqueIdentifier = UniqueIdentifier()) : LinearState, QueryableState {

override val participants: List<AbstractParty> = listOf(party)


override fun generateMappedObject(schema: MappedSchema): PersistentState {
    return when (schema) {
        SharingInformationSchemaV1 -> SharingInformationSchemaV1.PersistentSharingInformation(
                party,
                dataOwnerId,
                dataBuyerId,
                linearId.id
        )
        else -> throw IllegalArgumentException("Unrecognised schema $schema")
    }
}

override fun supportedSchemas(): Iterable<MappedSchema> = listOf(SharingInformationSchemaV1)

}

互联网上关于这个问题的信息很少。有人建议它连接到类路径,那里有重复的东西,但我不知道如何检查。此外,此错误与派对类型无关。我试图添加@JsonIgnore一个派对,但它会抛出另一个领域。该字段在映射模式中的持久性也无关紧要。我试过坚持而不坚持,它什么都没有改变。提前致谢!

标签: jsonspringspring-bootcordafasterxml

解决方案


我相信这是因为您缺少将 Corda 对象转换为 json 所需的 Corda Jackson 支持库。

将此添加到 build.gradle 中的依赖项中

compile "net.corda:corda-jackson:$corda_release_version"

https://github.com/corda/samples-java/blob/master/Advanced/auction-cordapp/client/build.gradle#L19

此外,请确保您已MappingJackson2HttpMessageConverter配置 bean。

@豆

public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
    ObjectMapper mapper =  JacksonSupport.createDefaultMapper(partyAProxy());
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(mapper);
    return converter;
}

https://github.com/corda/samples-java/blob/master/Advanced/auction-cordapp/client/src/main/java/net/corda/samples/client/AppConfig.java#L48


推荐阅读