我正在尝试将 Neo4j 服务器与 jersey 2 API 一起使用,但我很难将 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。应该如何构造请求>?
自己回答了这个问题:代码片段应该是:
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();