我正在使用Grails RestBuilder插件,但是当我尝试使用xml发送一个post请求时,会抛出异常:
Cannot cast object '<BookingRetrievalRQ xmlns="http://www.expediaconnect.com/EQC/BR/2014/01"><Authentication username="testuser" password="ECLPASS"/></BookingRetrievalRQ>'
with class 'java.lang.String' to class 'grails.converters.XML'
但我不明白为什么会这样。
这是我在服务类中的代码请求:
def resource = XmlUtil.marshal(msg)
def rest = new RestBuilder()
def resp = rest.post('https://simulator.expediaquickconnect.com/connect/br') {
contentType "application/xml"
xml resource
}
return XmlUtil.unMarshal(resp.xml, BookingRetrievalRS.class)
[XmlUtil.class]
static String marshal(def source) {
StringWriter writer = new StringWriter()
JAXBContext context = JAXBContext.newInstance(source.class)
Marshaller marshaller = context.createMarshaller()
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE)
marshaller.marshal(source, writer)
return writer.toString()
}
static def unMarshal(String strXml, Class targetClass){
JAXBContext context = JAXBContext.newInstance(targetClass)
def unMarshaller = context.createUnmarshaller()
return unMarshaller.unmarshal(new StringReader(strXml))
}
当我尝试这个时:
def rest = new RestBuilder()
def resp = rest.post("https://simulator.expediaquickconnect.com/connect/br") {
accept 'application/xml'
contentType 'application/xml'
xml {
BookingRetrievalRQ(xmlns: "http://www.expediaconnect.com/EQC/BR/2014/01") {
Authentication(username: "testuser", password: "ECLPASS")
}
}
}
我有以下错误消息:
unable to find valid certification path to requested target