首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于rest风格对Neo4j进行调用访问之———httpClient

基于rest风格对Neo4j进行调用访问之———httpClient

作者头像
MickyInvQ
发布2020-09-27 17:32:12
8050
发布2020-09-27 17:32:12
举报
文章被收录于专栏:InvQ的专栏InvQ的专栏

1.什么是rest风格

不知道戳这里

2.什么是Neo4j

简单来说是一个图形数据库,是一种NOSQL型的。具体摸我

3.httpclient 访问

参考官网链接摸我。 代码如下:

package neo4j.action;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import neo4j.config.RestTemplateConfiguration;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.HttpClient;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.*;


@RestController
@RequestMapping("/neo4j/data")
public class NeoController {
    private static final Logger log = LoggerFactory.getLogger(NeoController.class);

    @RequestMapping(value = "/getData", method = RequestMethod.POST)
    public String getHello() {
        //建立连接
        log.info("getData接口调用开始时间:"+new Date());// 接口调用开始时间
        String url = "http://localhost:7474/db/data/transaction/commit";
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials( new AuthScope("ip or host", port),
                new UsernamePasswordCredentials("username", "password"));
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();

        //参数准备以及封装请求
        StringEntity s = new StringEntity("{\n" +
                "  \"statements\" : [ {\n" +
                "    \"statement\" : \"MATCH (n) RETURN n LIMIT 25\"\n" +
                "  } ]\n" +
                "}","UTF-8");
        s.setContentEncoding("UTF-8");
        s.setContentType("application/json");//发送json数据需要设置contentType


        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(s);
        httppost.addHeader("Content-Type", "application/json");
        httppost.addHeader("charset", "UTF-8");
        CloseableHttpResponse response = null;

        //获取结果
        String respString = "";
        try {
            response = httpClient.execute(httppost);
             respString = EntityUtils.toString(response.getEntity());
            log.info(respString);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



        return respString;
    }
}

4,通过postman模拟点击结果返回

这里写图片描述
这里写图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-07-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.什么是rest风格
  • 2.什么是Neo4j
  • 3.httpclient 访问
  • 4,通过postman模拟点击结果返回
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档