2015-10-29 57 views
2

我用的Neo4j 2.2.4异常线程“main” com.sun.jersey.api.client.ClientHandlerException:java.net.ConnectException:连接被拒绝:连接

我尝试这个节目,这是我的测试代码

package neo4j.rest.client; 
import javax.ws.rs.core.MediaType; 
import org.json.JSONObject; 
import com.sun.jersey.api.client.Client; 
import com.sun.jersey.api.client.ClientResponse; 
import com.sun.jersey.api.client.WebResource; 
public class MyRestClient { 
public static void main(String[] args) { 
    MyRestClient jersey = new MyRestClient(); 
    jersey.createNode(); 
    jersey.sendCypher();   
} 
public void createNode(){ 
    //Create a REST Client 
    Client client = Client.create(); 
    //Define a resource (REST Endpoint) which needs to be Invoked 
    //for creating a Node 
    WebResource resource = client.resource("http://localhost:7474").path("/db/data/node"); 
    //Define properties for the node. 
    JSONObject node = new JSONObject(); 
    node.append("Name", "John");   
    System.out.println(node.toString());   
    //Invoke the rest endpoint as JSON request 
    ClientResponse res = resource.accept(MediaType.APPLICATION_JSON) 
    .entity(node.toString()) 
    .post(ClientResponse.class); 
    //Print the URI of the new Node 
    System.out.println("URI of New Node = " + res.getLocation());  
} 
public void sendCypher() { 
    //Create a REST Client 
    Client client = Client.create(); 
    //Define a resource (REST Endpoint) which needs to be Invoked 
    //for executing Cypher Query 
    WebResource resource = client.resource("http://localhost:7474").path("/db/data/cypher"); 
    //Define JSON Object and Cypher Query 
    JSONObject cypher = new JSONObject(); 
    cypher.accumulate("query", "match n return n limit 10"); 
    //Invoke the rest endpoint as JSON request 
    ClientResponse res = resource.accept(MediaType.APPLICATION_JSON).entity(cypher.toString()) 
    .post(ClientResponse.class); 
    //Print the response received from the Server 
    System.out.println(res.getEntity(String.class)); 
} 
} 

,这是我的错误信息,

{ “名称”: “约翰”]} 异常线程 “main” com.sun.jersey.api.client。 ClientHandlerException:java.net.ConnectException:连接被拒绝:c在com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149) 上com.sun.jersey.api.client.Client.handle(Client.java:648) com.sun上的onnect .jersey.api.client.WebResource.handle(WebResource.java:670) at com.sun.jersey.api.client.WebResource.access $ 200(WebResource.java:74) at com.sun.jersey.api。 client.WebResource $ Builder.post(WebResource.java:553) at neo4j.rest.client.MyRestClient.createNode(MyRestClient.java:30) at neo4j.rest.client.MyRestClient.main(MyRestClient.java:14) 原因:java.net.ConnectException:连接被拒绝:在java.net.DualStac上的java.net.DualStackPlainSocketImpl.connect0(Native Method) 处连接 kPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69) 在java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:337) 在java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198) 在java.net.AbstractPlainSocketImpl。在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157) 在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391) 在java.net.Socket.connect处(连接(AbstractPlainSocketImpl.java:180) Socket.java:579) at sun.net.NetworkClient.doConnect(NetworkClient.java:180) at sun.net.www.http.HttpClient.java.net.Socket.connect(Socket.java:528) at sun.net.NetworkClient.doConnect(NetworkClient.java:180) openserver(HttpClient.java:388) at sun.net.www.http.HttpCl (HttpClient.java:380) 在sun.net.www.http.HttpClient.New(HttpClient.java:316) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:992) at sun.net.www.protocol .http.HttpURLConnection.plainConnect(HttpURLConnection.java:928) at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:846) at sun.net.www.protocol.http.HttpURLConnection.getOutputStream (HttpURLConnection.java:1087) at com.sun.jersey.client.urlconnection.URLConnectionClientHandler $ 1 $ 1.getOutputStream(URLConnectionClientHandler.java:225) at com.sun.jersey.api.c lient.CommittingOutputStream.commitWrite(CommittingOutputStream.java:117) at com.sun.jersey.api.client.CommittingOutputStream.write(CommittingOutputStream.java:89) at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java: 221) at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291) at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:295) at sun.nio.cs.StreamEncoder.flush( StreamEncoder.java:141) at java.io.BufferedWriter.flush(BufferedWriter.java:254) at com.sun.jersey.core.util。java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229) ReaderWriter.writeToAsString(ReaderWriter.java:191) at com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider。writeToAsString(AbstractMessageReaderWriterProvider.java:128) at com.sun.jersey.core.impl.provider.entity.StringProvider.writeTo(StringProvider.java:88) at com.sun.jersey.core.impl.provider.entity。 StringProvider.writeTo(StringProvider.java:58) at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:300) at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler。 Java的:204) 在com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:147) ... 6个

谢谢。

+0

确定您的服务器正在运行并可通过http:// localhost:7474访问? – Luanne

+0

你是什么意思? –

回答

2
  • 检查您的Neo4j服务器是否已启动并正在运行。只需在浏览器中打开http://localhost:7474

  • 如果您使用Neo4j的默认设置,您的代码就可以工作。因为授权是默认启用的。你有两个选择:

    1. 添加授权头到您的要求

      client.addFilter(新HTTPBasicAuthFilter( “Neo4j的”, “your_password”));

    2. 禁用授权

neo4j-server.properties

# Disable authorization 
dbms.security.auth_enabled=false 
相关问题