我正在尝试将数据插入到neo4j图数据库中。我试图通过使用泽西客户端创建一个名为'Thamali'的Person类型节点。我已经给出了我在下面使用的java代码。
我尝试执行密码查询CREATE (thamali:Person {name:"Thamali"})
通过使用以下java代码。
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import javax.ws.rs.core.MediaType;
public class NeoAPIClass {
private final String baseUri = "http://localhost:7474/db/data/cypher";
private final String user = "neo4j";
private final String password = "12345";
public static void main(String args[]){
NeoAPIClass n=new NeoAPIClass();
n.runQuery();
}
void runQuery(){
Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter(user, password));
WebResource cypherResource = client.resource(baseUri);
String s="{\"query\":\"CREATE (thamali:Person{name:\"Thamali\"})\"}";
ClientResponse cypherResponse = cypherResource.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON_TYPE).entity(s).post(ClientResponse.class);
System.out.println("Output from Server .... "+ cypherResponse.getStatus());
}
}
我使用http状态代码500获得以下异常。
ERROR The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container Unexpected character ('T' (code 84)): was expecting comma to separate OBJECT entries [line: 1, column: 41]
org.neo4j.server.rest.repr.BadInputException: Unexpected character ('T' (code 84)): was expecting comma to separate OBJECT entries [line: 1, column: 41]
at org.neo4j.server.rest.repr.formats.JsonFormat.readMap(JsonFormat.java:94)
at org.neo4j.server.rest.web.CypherService.cypher(CypherService.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
Caused by: org.neo4j.server.rest.domain.JsonParseException: Unexpected character ('T' (code 84)): was expecting comma to separate OBJECT entries [line: 1, column: 41]
at org.neo4j.server.rest.domain.JsonHelper.readJson(JsonHelper.java:73)
at org.neo4j.server.rest.domain.JsonHelper.jsonToMap(JsonHelper.java:54)
at org.neo4j.server.rest.repr.formats.JsonFormat.readMap(JsonFormat.java:90)
... 43 more
任何人都可以帮我解决这个问题。
答案 0 :(得分:1)
不要直接调用Neo4j REST界面。目前,从java访问Neo4j服务器的推荐方法是使用其java驱动程序。
// imports
import org.neo4j.driver.v1.*;
import static org.neo4j.driver.v1.Values.parameters;
// init
Driver driver = GraphDatabase.driver( "bolt://localhost:7687", AuthTokens.basic( "neo4j", "neo4j" ) );
//run query
Session session = driver.session();
session.run( "CREATE (a:Person {name: {name}, title: {title}})",
parameters( "name", "Arthur", "title", "King" ) );
// destroy
session.close();
driver.close();
请参阅https://neo4j.com/developer/java/#_neo4j_for_java_developers
答案 1 :(得分:1)
正确形成JSON字符串的问题。两种解决方法:
1)内部报价的两个额外斜线:
String s="{\"query\":\"CREATE (thamali:Person{name:\\\"Thamali\\\"})\"}";
2)使用JSON库。例如Douglas Crockford:
JSONObject obj = new JSONObject();
obj.put("query", "CREATE (thamali: Person{name:\"Thamali\"})");
String s = obj.toString();