我的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
}
考虑到
query
方法的get()
参数是可选的,该参数用于查询方法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' ]
。希望能帮上忙,