1.什么是rest风格
不知道戳这里
2.什么是Neo4j
简单来说是一个图形数据库,是一种NOSQL型的。具体摸我
3.httpclient 访问
参考官网链接摸我。 代码如下:
代码语言:javascript复制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;
}
}