我希望有一个本地托管的Web服务,它将GET请求中的UserContact对象列表作为XML返回到URL http://localhost:8080/csvweb/contacts
我在URLMappings.groovy中有以下映射
"/contacts/$id"(controller: "contacts", parseRequest: true) {
action = [GET: "show", PUT: "update", DELETE: "delete", POST: "save"]
}
在联系人联系人控制器动作节目中定义如下
def show ={
if (params.id && UserContacts.exists(params.id.toLong())) {
def p = UserContacts.findById(params.id.toLong())
render p as XML
}
else {
def all = UserContacts.list()
render all as XML
}
}
如何从groovy控制台使用HttpBuilder发出GET请求?我到现在为止这个
@Grab( 'org.codehaus.groovy.modules.http-builder:http-builder:0.5.2-SNAPSHOT' )
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.GET
def http = new HTTPBuilder("http://localhost:8080/csvwebservice")
http.handler.failure = { resp, xml ->
println "it broke ${resp} ${xml}"
}
http.get(path: '/contacts') { resp, xml ->
if (resp.status == 200) {
xml?.items.each {UserContacts ->
println "${UserContacts?.email}"
}
} else {
return [error:"Did not return a proper response: ${resp.status}"]
}
}
我总是收到此错误
groovyx.net.http.HTTPBuilder parseResponse
WARNING: Could not parse content-type: Response does not have a content-type header
it broke groovyx.net.http.HttpResponseDecorator@62f6fb59
编辑:添加contentType:ContentType.xml后,我得到以下异常: 2012年2月29日下午2:07:45 groovyx.net.http.ParserRegistry getCharset
WARNING: Could not parse charset from content-type header in response
[Fatal Error] :-1:-1: Premature end of file.
Feb 29, 2012 2:07:45 PM groovyx.net.http.HTTPBuilder doRequest
WARNING: Error parsing 'null' response
org.codehaus.groovy.runtime.InvokerInvocationException: org.xml.sax.SAXParseException: Premature end of file.
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1058)
.....
Edit2:来自webbrowser的webservice的响应
<list>
<userContacts id="1">
<company id="1"/>
<email>jonas.sultani@flintstones.com</email>
<fax>+49 6245 95535</fax>
<firstName>Jonas Sultani</firstName>
<lastName/>
<phone>+49 6245 99555</phone>
<prefix>Mr</prefix>
<title>Consultant</title>
</userContacts>
<userContacts id="2"><company id="2"/><email>james.simmons@airplanes.com</email><fax/><firstName>James H</firstName><lastName>Simmons</lastName><phone>+1 112-434-6684</phone><prefix>Mr</prefix><title>AP Lead</title>
</userContacts>
<userContacts id="3"><company id="3"/><email>slmarino@bowringer</email><fax/><firstName>Stephanie</firstName><lastName>Marino</lastName><phone>+1 650-544-5444</phone><prefix>Mrs</prefix><title>Project Manager</title>
</userContacts>
</list>