【问题标题】:Neo4j - Server returned HTTP response code: 500 for URL: http://localhost:7474/db/data/cypherNeo4j - 服务器返回 HTTP 响应代码:500 用于 URL:http://localhost:7474/db/data/cypher
【发布时间】:2015-02-06 16:57:08
【问题描述】:

我在使用 HTTPConnection 运行密码时遇到异常。

服务器返回 HTTP 响应代码:500 对应 URL:http://localhost:7474/db/data/cypher

public class HTTPConnectionTest {
    public static void main(String[] args) throws Exception {
        try {
                System.out.println("Testing HTTPConnection");
                StringBuffer responseString = new StringBuffer();
                String url = "http://localhost:7474/db/data/cypher";

                //String query = "match (user:USER{id:\'Sree\'}) return user ";
                String query = "match (user:USER{id:\"Sree\"}) return user ";
                URL neo4jUrl = new URL(url);
                HttpURLConnection httpConn = (HttpURLConnection) neo4jUrl
                        .openConnection();
                httpConn.setRequestMethod("POST");
                httpConn.setDoOutput(true);
                httpConn.setDoInput(true);
                httpConn.setRequestProperty("Content-Type", "application/json");
                String urlParameters = "{\"query\":\"" + query + "\"}";
                httpConn.setRequestProperty("Accept",
                        "application/json; charset=UTF-8");

                DataOutputStream wr = new DataOutputStream(
                        httpConn.getOutputStream());
                wr.writeBytes(urlParameters);
                wr.flush();
                wr.close();

                BufferedReader in = new BufferedReader(new InputStreamReader(
                        httpConn.getInputStream()));
                String inputLine;

                while ((inputLine = in.readLine()) != null) {
                    responseString.append(inputLine);
                }
                System.out.println("Out put " + responseString);
                in.close();
            } catch (Exception e) {
                System.out.println("Exception" + e.getMessage());
            }
    }
}

如果我用单引号在密码中传递值,它正在工作并获取输出对象。

字符串查询 = "匹配 (user:USER{id:\'Sree\'}) 返回用户 ";

有什么建议吗?提前谢谢!

【问题讨论】:

    标签: java neo4j cypher


    【解决方案1】:

    这在我看来很愚蠢。

    代码中的一个小改动。将参数转换为 JSONObject 字符串

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("query", query);
    String urlParameters = jsonObject.toString();
    

    而不是

    String urlParameters = "{\"query\":\"" + query + "\"}";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-29
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      相关资源
      最近更新 更多