我的 YouTrack 服务器中存储了多个问题,我想提取这些问题并将信息打包到字符串中。
我已经使用过 RESTClient,但是我得到了错误的输出,因此想尝试一种不同的方法来使用 HttpBuilder 提取问题并在 JSON 中格式化 xml 响应。但是我还不知道如何在 Groovy 中做到这一点(可能是因为我缺少一个完整的、正在运行的示例):
经过 this website和 this
我希望我的代码看起来像这样:
def http = new HTTPBuilder('http://www.MyYouTrackServer.com')
AuthenticateMe() // I need that, otherwise I cannot access my server
http.get( path : 'MyIssue-25',
contentType : JSON,
query : [???'] ) { resp, reader ->
....
// This gap has to be filled somehow,
// so that I have a JSONObject or JSONArray, I can work with.
....
}
println 'Response data: -----'
System.out << reader
println '\n--------------------'
}
String str; // this is the important String containing the data
欢迎任何建设性的建议、回答或评论。
然后响应将如下所示:
<issues>
<issue>
<comment created="1277899067543" text="is it something wrong?" author="root"/>
<field name="numberInProject"><value>0</value></field>
<field name="summary"><value>susjs</value></field>
<field name="priority"><value>1</value></field>
<field name="description"><value>at jsjsjsj.mps.E.java at line 12</value></field>
<field name="created"><value>1277392628191</value></field>
<field name="updated"><value>1277399118476</value></field>
<field name="reporterName"><value>root</value></field>
<field name="updaterName"><value>root</value></field>
<field name="state"><value>Submitted</value></field>
<field name="subsystem"><value>No subsystem</value></field>
<field name="fixedInBuild"><value>Next build</value></field>
<field name="permittedGroup"><value>All Users</value></field>
</issue>
</issues>
最佳答案
为了实现您的目标,您可以使用以下方法来处理 json:
import groovyx.net.http.HTTPBuilder
def http = new HTTPBuilder('http://www.MyYouTrackServer.com')
...
http.get( path : '/MyIssue-25',
contentType : 'application/json'
) { resp, reader ->
// inside reader you've your json object in `net.sf.json.JSONObject` instance
println reader
}
请注意 get()
方法的 query
参数是可选的,此参数用于查询方法 url,如 https://twitter.com/search?q=asd
,对于这种情况,查询参数将为 query : [ q : 'asd' ]
。
所以回到代码,在 reader
对象中你有一个 net.sf.json.JSONObject
的实例。一起工作,看看its API .
举个小例子,我在 http://localhost/index.json 有一个服务器它返回以下 json { "a":"a", "b": { "b1":"b1", "b2":"b2"}, "c":"c"}
为了与我一起工作,我使用以下代码:
import groovyx.net.http.HTTPBuilder
def http = new HTTPBuilder('http://localhost')
http.get( path : '/index.json',
contentType : 'application/json'
) { resp, reader ->
// cast the object it's not necessary... I cast it
// to have the method suggestions by IDE
net.sf.json.JSONObject read = reader
println read.get("a") // prints "a"
println read.get("b").get("b1") // prints "b1"
//...
// you can also use this approach
println read.a // prints "a"
println read.b.b1 // prints "b1"
println read.b // prints [b1:b1, b2:b2]
}
更新
我再次阅读了您的问题,您的描述似乎是您正在尝试阅读 YourTrack 中的问题以 xml
格式。为此,它与 json
非常相似,在这种情况下,reader
对象是 GPathResult
的一个实例。看看下面的示例,假设您的回答看起来像您在问题中提出的回答:
http = new HTTPBuilder('http://www.MyYouTrackServer.com')
http.get( path : '/MyIssue-25',
contentType : 'application/xml'
) { resp, reader ->
// since your response is an xml now in reader you've GPathResult
// and now some samples on how to work with the response:
// get the text of each <field>
def fields = reader.issue.field*.text();
fields.each {
print "$it " // prints 0 susjs 1 at jsjsjsj.mps.E.java at line 12 1277392628191 1277399118476 root root Submitted No subsystem Next build All Users numberInProject
}
// another sample... get the name attribute value for the <field> elements
def namesAttr = reader.issue.field*.@name
namesAttr.each {
print "$it " // prints numberInProject summary priority description created updated reporterName updaterName state subsystem fixedInBuild permittedGroup
}
// find the <field> value for element which has attribute name="state"
def field = reader.issue.'*'.findAll {
it.@name == 'state'
}
println field.text() // prints submitted
}
也在这个YourTrack
operation似乎有两个查询参数(project 和 max)可以使用它,您可以将查询参数添加到 get()
方法,即:query : [ max : '15' ]
.
希望对您有所帮助,
关于html - 如何使用 HTTPBuilder 在 Groovy 中设置 GET-Response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32055859/