我熟悉像这些正常的常规闭包
def printSum = {a,b ->
println a+b
}
printSum(5,7) // 12
但是,我遇到了 SpringWS 插件的代码,我很难理解:
def withEndpointRequest = { url, payload ->
def writer = new StringWriter()
def request = new MarkupBuilder(writer)
payload.delegate = request
payload.call()
def webServiceTemplate = new WebServiceTemplate()
def response = webServiceTemplate.sendToEndpoint(url, writer.toString())
new XmlSlurper().parseText(response)
}
我知道上面是一个闭包。
它是这样使用的:
def namespace = "http://www.myveryimportantcompany.com/hr/schemas"
def serviceURL = "http://localhost:8080/myapp/services"
def response = withEndpointRequest(serviceURL) {
HolidayRequest(xmlns: namespace) {
Holiday {
StartDate("2006-07-03")
EndDate("2006-07-07")
}
Employee {
Number("42")
FirstName("Russ")
LastName("Miles")
}
}
}
如果serviceURL
正在传递,那么有效载荷在哪里?
有人可以详细解释一下这个片段吗?