json - 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
一个派对,但它会抛出另一个领域。该字段在映射模式中的持久性也无关紧要。我试过坚持而不坚持,它什么都没有改变。提前致谢!
解决方案
我相信这是因为您缺少将 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;
}
推荐阅读
- javascript - 使用 for 循环和函数的 JavaScript 问题
- hyperledger - Composer 休息服务器实体太大
- swift - 为什么我的 UIImage 没有出现在我的 tabBar 中?
- npm - npm 命令行界面将版本号注入文件
- visual-studio - Visual Studio ftp 发布
- mysql - MariaDB 在创建过程时显示错误
- mysql - MYSQL(MariaDB)在更新时计算百分比列
- c++ - 程序如何知道静态变量是否需要初始化?
- latex - nomencl 包的问题 [LATEX]
- r - 在数据帧上执行 expand.grid() 时出错