在 java 代码中向密码查询插入字符串参数时出错
Error in inserting a string parameter to a cypher query in a java code
我想在 Java 中向密码查询插入一个字符串参数。下面是我使用的代码,我有一个名为 'piyumi' 的人节点,我想与一个 activity 节点建立关系。 activity 节点的名称是 'walking'。当我执行代码时,我得到 http 状态代码 400。任何人都可以帮我修改密码查询,以便我可以无误地插入字符串变量 s。
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 org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
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[]) throws JSONException {
NeoAPIClass n=new NeoAPIClass();
n.runQuery();
}
void runQuery() throws JSONException {
Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter(user, password));
WebResource cypherResource = client.resource(baseUri);
String s="walking";
JSONObject obj = new JSONObject();
obj.put("query", "Match(piyumi : Person{name:\"Piyumi\"}) create (piyumi)-[:doing{timestamp:4789}]->(n:activity) WHERE n.name=s");
String st = obj.toString();
ClientResponse cypherResponse = cypherResource.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON_TYPE).entity(st).post(ClientResponse.class);
System.out.println("Output from Server .... "+ cypherResponse.getStatus());
}
}
您的问题是密码服务器无法从您的 Java 代码中看到变量 s
。您需要传递具有 String
本身的密码查询,而不是 Java 变量的名称。
您可以将带有 obj.put
的行更改为此。
obj.put("query", "Match(piyumi : Person{name:\"Piyumi\"}) create (piyumi)-[:doing{timestamp:4789}]->(n:activity) WHERE n.name=\"" + s + "\"");
当您尝试检索现有节点时出现问题。
因此,转到 http://localhost:7474/browser/ 并执行以下查询,
匹配(n:activity)
其中 n.name="walking"
创建 (piyumi:Person{name:"Piyumi"}) - [r1:doing{timestamp:4789}]-> (n)
如果没问题,再取回。
匹配 (piyumi:Person)-[r1:doing]->(n:activity)
RETURNr1
所以你的查询代码应该是这样的,
"MATCH (n:activity)\nWHERE n.name=\"walking\"\nCREATE (piyumi:Person{name:\"Piyumi\"}) - [r1:doing{timestamp:4789}]-> (n)"
create
和 where
无法组合。您需要先匹配人和 activity,然后创建关系。
并且还建议在请求的单独部分中简化传输参数 (https://neo4j.com/docs/rest-docs/current/#rest-api-use-parameters):
JSONObject request = new JSONObject();
JSONObject params = new JSONObject();
String query = "MATCH (P:Person { name:{personName} }) \n";
query = query + "MATCH (A:activity { name:{activityName} }) \n";
query = query + "CREATE (P)-[rel:doing { timestamp:{activityTimestamp} }]->(A) \n";
query = query + "RETURN P, A, rel";
request.put("query", query);
params.put("personName", "Piyumi");
params.put("activityName", "walking");
params.put("activityTimestamp", 4789);
request.put("params", params);
ClientResponse cypherResponse = cypherResource.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(request.toString())
.post(ClientResponse.class);
System.out.println("Response: " + cypherResponse.getEntity(String.class));
我想在 Java 中向密码查询插入一个字符串参数。下面是我使用的代码,我有一个名为 'piyumi' 的人节点,我想与一个 activity 节点建立关系。 activity 节点的名称是 'walking'。当我执行代码时,我得到 http 状态代码 400。任何人都可以帮我修改密码查询,以便我可以无误地插入字符串变量 s。
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 org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
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[]) throws JSONException {
NeoAPIClass n=new NeoAPIClass();
n.runQuery();
}
void runQuery() throws JSONException {
Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter(user, password));
WebResource cypherResource = client.resource(baseUri);
String s="walking";
JSONObject obj = new JSONObject();
obj.put("query", "Match(piyumi : Person{name:\"Piyumi\"}) create (piyumi)-[:doing{timestamp:4789}]->(n:activity) WHERE n.name=s");
String st = obj.toString();
ClientResponse cypherResponse = cypherResource.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON_TYPE).entity(st).post(ClientResponse.class);
System.out.println("Output from Server .... "+ cypherResponse.getStatus());
}
}
您的问题是密码服务器无法从您的 Java 代码中看到变量 s
。您需要传递具有 String
本身的密码查询,而不是 Java 变量的名称。
您可以将带有 obj.put
的行更改为此。
obj.put("query", "Match(piyumi : Person{name:\"Piyumi\"}) create (piyumi)-[:doing{timestamp:4789}]->(n:activity) WHERE n.name=\"" + s + "\"");
当您尝试检索现有节点时出现问题。 因此,转到 http://localhost:7474/browser/ 并执行以下查询,
匹配(n:activity) 其中 n.name="walking" 创建 (piyumi:Person{name:"Piyumi"}) - [r1:doing{timestamp:4789}]-> (n)
如果没问题,再取回。 匹配 (piyumi:Person)-[r1:doing]->(n:activity) RETURNr1
所以你的查询代码应该是这样的,
"MATCH (n:activity)\nWHERE n.name=\"walking\"\nCREATE (piyumi:Person{name:\"Piyumi\"}) - [r1:doing{timestamp:4789}]-> (n)"
create
和 where
无法组合。您需要先匹配人和 activity,然后创建关系。
并且还建议在请求的单独部分中简化传输参数 (https://neo4j.com/docs/rest-docs/current/#rest-api-use-parameters):
JSONObject request = new JSONObject();
JSONObject params = new JSONObject();
String query = "MATCH (P:Person { name:{personName} }) \n";
query = query + "MATCH (A:activity { name:{activityName} }) \n";
query = query + "CREATE (P)-[rel:doing { timestamp:{activityTimestamp} }]->(A) \n";
query = query + "RETURN P, A, rel";
request.put("query", query);
params.put("personName", "Piyumi");
params.put("activityName", "walking");
params.put("activityTimestamp", 4789);
request.put("params", params);
ClientResponse cypherResponse = cypherResource.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(request.toString())
.post(ClientResponse.class);
System.out.println("Response: " + cypherResponse.getEntity(String.class));