具有Spring Boot的CordaApp Api响应不同

时间:2018-04-04 13:08:44

标签: corda

我已将cordapp与spring boot集成。一个奇怪的观察我们发现响应不同来自cordawebserver和spring boot服务器,例如

  

块引用

API:GET:market / me 给出

{
 “me”: {
   “commonName”: null,
   “organisationUnit”: null,
   “organisation”: “PartyG-CT”,
   “locality”: “Tokyo”,
   “state”: null,
   “country”: “JP”,
   “x500Principal”: {
     “name”: “O=PartyG-CT,L=Tokyo,C=JP”,
     “encoded”: “MDExCzAJBgNVBAYTAkpQMQ4wDAYDVQQHDAVUb2t5bzESMBAGA1UECgwJUGFydHlHLUNU”
   }
 }
}

用Spring启动 我们正在使用cordawebserver:

   {
 “me”: “C=JP,L=Tokyo,O=PartyG-CT”
}

我们为不同的API发现相同的行为任何帮助将不胜感激

@GET
        @Path("peers")
        @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
   // CordaX500Name
        public Map<String, List<CordaX500Name>> getPeers() {
            List<NodeInfo> nodeInfoSnapshot = proxy.networkMapSnapshot();
            return ImmutableMap.of(
                    "peers",
                    nodeInfoSnapshot
                            .stream()
                            .map(node -> node.getLegalIdentities().get(0).getName())
                            .filter(name -> !name.equals(myLegalName) && !name.getOrganisation().equals(controllerName)
                                    && !name.getOrganisation().equals(NETWORK_MAP_NAME))
                            .collect(toList()));
        }


//boot entry point
    @SpringBootApplication
    public class FacilityServer  {


        @Autowired
        public  static NodeRPCConnection nodeRPCConnection;
        /**
         * Starts our Spring Boot application.
         */
        public static void main(String[] args) throws Exception {
            SpringApplication springApplication = new SpringApplication();
            springApplication.setBannerMode(Banner.Mode.OFF);
            springApplication.run(FacilityServer.class, args);
        }

        @EventListener(ApplicationReadyEvent.class)
        public void initiateFacilityObserverPostStartup() throws Exception{

            FacilityObserver.startFacilityWatch();

        }



// this class for using the jersey instead of spring rest impltn
@Configuration
@ApplicationPath("rest")
public class JerseyConfig extends ResourceConfig {



    public JerseyConfig() {



    }

    @PostConstruct
    public void setUp() {
        register(MarketApi.class);
        //register(GenericExceptionMapper.class);
    }
}

1 个答案:

答案 0 :(得分:1)

我找到了解决方案;根据corda文档,我们需要注册cordajackson mapper。然后,我们将能够获得与corda webserer相同的响应。 例如:

List<StateAndRef<YourState>> vaultStatesList =  vaultStates.getStates();  

ObjectMapper mapper = JacksonSupport.createNonRpcMapper();
String json = mapper.writeValueAsString(vaultStatesList);