java连接neo4j之rest api

neo4j连接java目前主要有嵌入式、jdbc和rest api。

以neo4j文档的Jersey为例(实际有多种方式可以实现,目前觉得Jersey实现比较麻烦点,其他的都有封装好请求)。

使用的lib包:jersey-bundle-1.17.jar(这个比较不好找)和Jersey提供的包

    String SERVER_ROOT_URI = "http://localhost:7474/db/data/";
        final String nodeEntryPointUri = SERVER_ROOT_URI + "node";
        WebResource resource = Client.create()
                .resource( nodeEntryPointUri );
        // POST {} to the node entry point URI
        //通过post请求新建一个节点
        ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
                .type( MediaType.APPLICATION_JSON )
                .entity( "{}" )
                .post( ClientResponse.class );
         
        final URI location = response.getLocation();
        System.out.println( String.format(
                "POST to [%s], status code [%d], location header [%s]",
                nodeEntryPointUri, response.getStatus(), location.toString() ) );
        response.close();
        System.out.println(location.toString());
        
        String propertyUri = location.toString() + "/properties/" + "name";
        // http://localhost:7474/db/data/node/{node_id}/properties/{property_name}
         
        WebResource propertyResource = Client.create()
                .resource( propertyUri );
        //根据拿回来的节点url,设置节点的属性
        ClientResponse response1 = propertyResource.accept( MediaType.APPLICATION_JSON )
                .type( MediaType.APPLICATION_JSON )
                .entity( "\"Joe Strummer\"")
                .put( ClientResponse.class );
         
        System.out.println( String.format( "PUT to [%s], status code [%d]",
                propertyUri, response1.getStatus() ) );
        response.close();

jersey这种方式个人觉得不是很好使用,在创建一个节点时不能把属性加入进去,必须是{},然后在根据拿回来的节点url加入属性,比较繁琐,但是根据权重计算最短路径时,如果想使用服务式的neo4j。

转载于:https://www.cnblogs.com/visoncheng/p/4148950.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值