gpt4 book ai didi

java - 使用java代码中的字符串执行密码查询时出现异常

转载 作者:行者123 更新时间:2023-11-30 02:41:17 25 4
gpt4 key购买 nike

我正在尝试将数据插入 Neo4j 图形数据库。我正在尝试使用 Jersey 客户端创建一个名称为“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

谁能帮我解决这个问题吗?

最佳答案

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

关于java - 使用java代码中的字符串执行密码查询时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41627726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com