简体   繁体   English

使用来自Java代码的字符串执行密码查询时发生异常

[英]Exception when executing cypher query with a string from a java code

I am trying to insert data to a neo4j graph database. 我正在尝试将数据插入neo4j图形数据库。 I am trying to create a Person type node whose name is 'Thamali' by using a jersey client. 我正在尝试使用球衣客户端创建一个名称为'Thamali'的Person类型节点。 I have given the java code that I use below. 我在下面给出了我使用的Java代码。 I try to execute the cypher query CREATE (thamali:Person {name:"Thamali"}) By using the below java code. 我尝试使用以下Java代码执行密码查询CREATE (thamali:Person {name:"Thamali"})

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());

    }

}

I get the following exception with http status code 500. 我收到以下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

Can anyone please help me to solve this. 谁能帮我解决这个问题。

Do not call Neo4j REST interface directly. 不要直接调用Neo4j REST接口。 Currently the recommended way to access Neo4j server from java is to use its java driver. 当前,从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();

See https://neo4j.com/developer/java/#_neo4j_for_java_developers 参见https://neo4j.com/developer/java/#_neo4j_for_java_developers

The problem in the correct formation of the JSON-string. JSON字符串格式正确的问题。 Two ways to solve: 两种解决方法:

1) Two additional slash to the internal quotation: 1)在内部引号中再加上两个斜杠:

String s="{\"query\":\"CREATE (thamali:Person{name:\\\"Thamali\\\"})\"}";

2) Use JSON library. 2)使用JSON库。 For example by Douglas Crockford : 例如, 道格拉斯·克罗克福德Douglas Crockford)

JSONObject obj = new JSONObject();
obj.put("query", "CREATE (thamali: Person{name:\"Thamali\"})");
String s = obj.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM