Corda RPC JacksonSupport.createDefaultMapper to use ObjectMapper in Spring client

南楼画角 提交于 2019-12-11 06:55:07

问题


How to register newly introduced Corda RPC ObjectMapper in Spring Boot?

Even after having below code in @Configuration class Jackson failing to serialize Party object to JSON string.

@Bean
public JsonComponentModule jsonComponentModule() {
    return new JsonComponentModule();
}

@Bean
@Primary
public ObjectMapper cordaRpcObjectMapper(NodeRPCConnection rpc) {
    ObjectMapper objectMapper = JacksonSupport.createDefaultMapper(rpc.getProxy(), new JsonFactory(), true);
    objectMapper.registerModule(jsonComponentModule());
    return objectMapper;
}

回答1:


After some tweaks I'm successfully able to register Corda RPC ObjectMapper with Jackson with below code.

//Register any other custom (de)Serializer classes.
@Bean
public Module jsonComponentModule() {
    return new JsonComponentModule();
}

//Force Spring/Jackson to use only provided Corda ObjectMapper for serialization.
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(@Autowired NodeRPCConnection rpcConnection) {
    ObjectMapper mapper = JacksonSupport.createDefaultMapper(rpcConnection.getProxy()/*, new JsonFactory(), true*/);
    mapper.registerModule(jsonComponentModule());

    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(mapper);
    return converter;
}


来源:https://stackoverflow.com/questions/51895899/corda-rpc-jacksonsupport-createdefaultmapper-to-use-objectmapper-in-spring-clien

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!