gpt4 book ai didi

grails - 使用Rest Client Builder在图数据库上运行密码查询的Grails查询

转载 作者:行者123 更新时间:2023-12-02 14:44:29 25 4
gpt4 key购买 nike

尝试使用此处找到的rest-client-builder插件

http://grails.org/plugin/rest-client-builder

在我的neo4j图上运行密码查询

因此,我构建了一个符合我想要的功能的常规脚本

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2' )
@Grab(group='net.sf.json-lib', module='json-lib', version='2.4', classifier='jdk15' )

import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*

def query(statement, params,success, error) {
def http = new HTTPBuilder( 'http://localhost:7474' )
http.request( POST, JSON ) {
uri.path = '/db/data/cypher/'
headers.'X-Stream' = 'true'
requestContentType = JSON
body = [ query : statement , params : params ?: [:] ]


response.success = { resp, json ->
if (success) success(json)
else {
println "Status ${resp.statusLine} Columns ${json.columns}\nData: ${json.data}"
}
}

response.failure = { resp, message ->
def result=[status:resp.statusLine.statusCode,statusText:resp.statusLine.reasonPhrase]
result.headers = resp.headers.collect { h -> [ (h.name) : h.value ] }
result.message = message
if (error) {
error(result)
} else {
println "Status: ${result.status} : ${result.statusText} "
println 'Headers: ${result.headers}'
println 'Message: ${result.message}'
}
}
}
}

query("START v=node({id}) RETURN v",[id:170],{ println "Success: ${it.data}" },{ println "Error: ${it}" })

这将返回我想要的json。所以我在用 Controller 方法复制此命令时遇到了麻烦,因此我选择了一个可以执行http rest post请求的插件。

因此,我有一个称为 CypherService的服务方法:
package awhinterface
import grails.converters.JSON
import grails.plugins.rest.client.RestBuilder

class CypherService {

def query() {
def rest = new RestBuilder()
def resp = rest.post("http://localhost:7474"){
contentType "application/json"
body = [query: "START v=node(170) RETURN v"]

}
return resp as JSON;
}
}

我为此编写了一个非常简单的测试:
import grails.test.mixin.*
import org.junit.*
@TestFor(CypherService)
class CypherServiceTests {
void testquery() {
def cypherService = new CypherService()
def myjson = cypherService.query()
println(myjson)
}
}

但是,我留下了这个错误:
Could not write request: no suitable HttpMessageConverter found for request type   [org.springframework.util.LinkedMultiValueMap] and content type [application/json]
org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type [application/json]
at grails.plugins.rest.client.RestBuilder.doRequestInternal(RestBuilder.groovy:93)
at grails.plugins.rest.client.RestBuilder.post(RestBuilder.groovy:72)
at awhinterface.CypherService.query(CypherService.groovy:10)
at awhinterface.CypherServiceTests.testquery(CypherServiceTests.groovy:17)

我对HTTP请求仍然很陌生。有人煽动吗?谢谢!

编辑

目前提出了一些建议并得到了这个
def query() {
def rest = new RestBuilder()
def resp = rest.post("http://localhost:7474") {
contentType "application/json"
uri = "/db/data/cypher/"
body '{query: "START v=node(170) RETURN v"}'
}
return resp

出现此错误:
org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type [application/json]
at grails.plugins.rest.client.RestBuilder.doRequestInternal(RestBuilder.groovy:93)
at grails.plugins.rest.client.RestBuilder.post(RestBuilder.groovy:72)

最佳答案

@dmahapatro的答案非常接近,但是您必须省略contentType设置。服务方法应如下所示:

def query( )
{
def rest = new RestBuilder( )
def resp = rest.post( "http://localhost:7474/db/data/cypher" ) {
headers.'X-Stream' = 'true'
query = "START v=node(170) RETURN id(v)"
}
return resp.json;
}

返回值是一个包含 datacolumns键的映射。

旁注:要摆脱 no suitable HttpMessageConverter found for request type XXX,请尝试将jar依赖项添加到BuildConfig.groovy:
compile 'com.fasterxml.jackson.core:jackson-databind:2.2.2'

更新资料

如果您想使用参数化的密码,那会有些棘手。核心问题是RestBuilder在内部实例化一个没有参数的JsonBuilder,但是在这种情况下,您需要使用参数,因为您的json现在是一个映射,而不是树状结构。为您的服务使用以下代码段:
import grails.plugins.rest.client.RestBuilder
import groovy.json.JsonBuilder

class CypherService {

def query() {
def rest = new RestBuilder()
def resp = rest.post( "http://localhost:7474/db/data/cypher" ) {
headers.'X-Stream' = 'true'
body(new JsonBuilder( query: "START v=node({nodeId}) RETURN id(v)",params: [ nodeId: 1]).toString())
}
return resp.json;
}

}

关于grails - 使用Rest Client Builder在图数据库上运行密码查询的Grails查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18025649/

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