使用Neo4j和Jersey 2.x API

时间:2013-11-06 13:19:24

标签: java jersey neo4j jersey-2.0

我正在尝试使用带有jersey 2 API的Neo4j服务器,但我很难将neo4j中的示例从jersey 1.x重写为2.x.例如。这里的第一个例子是:

WebResource resource = Client.create().resource( SERVER_ROOT_URI );
ClientResponse response = resource.get( ClientResponse.class );
System.out.println( String.format( "GET on [%s], status code [%d]",SERVER_ROOT_URI, response.getStatus() ) );
response.close();

可以改写为:

String baseURI = new String("http://localhost:7474/");
Client client = ClientBuilder.newClient();
WebTarget target = client.target(baseURI);
Invocation.Builder invocationBuilder = target.request(MediaType.TEXT_PLAIN_TYPE);
Response response = invocationBuilder.get();

但是当我尝试重写第二个例子时,我被卡住了:

final String nodeEntryPointUri = SERVER_ROOT_URI + "node"; //http://localhost:7474/db/data/node
WebResource resource = Client.create().resource( nodeEntryPointUri );
// POST {} to the node entry point URI
ClientResponse response = resource.accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON ).entity( "{}" ).post( ClientResponse.class );

应该看起来像:

String baseURI = new String("http://localhost:7474/");
Client client = ClientBuilder.newClient();
WebTarget target = client.target(baseURI).path(newnode);
Response response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity("{}", MediaType.APPLICATION_JSON_TYPE));
System.out.println(postResponse.getStatus());

但这会返回http 404.请求的结构应该如何>?

我自己回答:COde片段应该是:

String baseURI = new String("http://localhost:7474/"); 
Client client2 = ClientBuilder.newClient();
WebTarget target2 = client2.target(baseURI + "db/data/node");
System.out.println(target2.getUri());
Response postResponse = target2.request().accept(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(JSON , MediaType.APPLICATION_JSON_TYPE));
    System.out.println(postResponse.getStatus() + "     " + postResponse.getLocation());
    postResponse.close();

0 个答案:

没有答案