基於rest風格對Neo4j進行呼叫訪問之———httpClient

基於rest風格對Neo4j進行呼叫訪問之———httpClient

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模擬點選結果返回

這裡寫圖片描述