【问题标题】:How to PUT XmlSlurper back to REST with HttpBuilder如何使用 HttpBuilder 将 XmlSlurper 放回 REST
【发布时间】:2014-12-28 12:43:55
【问题描述】:

我正在尝试对 XML REST Web 服务进行 GET 然后 PUT 调用。

我是这样做的:

@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7')
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
import groovy.xml.XmlUtil

def url = "http://localhost:81"
def pathPrefix = "/api/v1"

def http = new HTTPBuilder(url)
def profile = http.request(GET, XML) { req ->
    uri.path = "$pathPrefix/profiles/55"
    response.success = {resp, xml ->
        xml
    }
}

println XmlUtil.serialize(profile) // this is fine!

现在我要更改并保存

profile.name = "New Name"

// this is not fine (i have 400 Bad Request)
// because it sends body not in XML
def savedProfile = http.request(PUT, XML) { req ->
    uri.path = "$pathPrefix/profiles/55"
    body = profile
    response.success = {resp, xml ->
        xml
    }
}

println XmlUtil.serialize(savedProfile)

当我发出 PUT 请求时,HTTPBuilder 不发送 XML。它发送由 profile.toString() 组成的字符串。

这不是我所期望的。 如何在 PUT 请求中发送 XmlSlurper 对象(我之前获得的)?

谢谢。

【问题讨论】:

    标签: rest groovy httpbuilder xmlslurper http-put


    【解决方案1】:

    我想我找到了解决方案。 当我定义身体配置值时,我必须写

    body = {
        mkp.yield profile
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多