我遇到了将我的SOAP请求从CXF有效负载格式转换为所需格式的问题。
这是请求的样子:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<ObscureSOAPOperation xmlns="<wsdl-namespace-url>">
<ObjectInfo>
<value1>value1</value1>
<value2>value2</value2>
</ObjectInfo>
</ObscureSOAPOperation>
</soapenv:Body>
</soapenv:Envelope>
这就是我的路线:
from("properties:soapoperation.service.cxf.endpoint")
.to("log:info?showAll=true")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
CxfPayload<?> request = (CxfPayload<?>) exchange.getIn().getBody();
Source source = request.getBodySources().get(0);
JAXBElement<ObjectInfo> objectInfoElement =
jaxbContext.createUnmarshaller().unmarshal(source, ObjectInfo.class);
System.out.println("~~~~~~~~~Object Info value1: " + objectInfoElement.getValue().getValue1() + "~~~~~~~~~");
}
})
ObjectInfo
是WSDL生成的类。顺便提一下,WSDL是一个rpc / literal样式的wsdl。
问题是当来自交换的请求被转换为CxfPayload时。它变为空。 DOMSource看起来像:
<ObjectInfo>
<value1>null</value1>
<value2>null</value2>
</ObjectInfo>
我的SOAP请求实际上在ObjectInfo
之后包含了几个元素(WSDL具有针对特定SOAP请求的多部分消息),这些元素也是空的。
答案 0 :(得分:0)
我找到了解决问题的临时工作。
我使用XmlConverter将主体转换为String,而不是使用Source。然后将其解组为所需的对象类型。
@Override
public void process(Exchange exchange) throws Exception {
CxfPayload<SoapHeader> request = exchange.getIn().getBody(CxfPayload.class);
XmlConverter converter = new XmlConverter();
String xmlInRequest = converter.toString(request.getBody().get(0).cloneNode(true), exchange);
xmlInRequest = xmlInRequest.replace(" xmlns=\"<wsdl-namespace-url>\"", "");
xmlInRequest = xmlInRequest.replace(" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"", "");
xmlInRequest = xmlInRequest.replace(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", "");
xmlInRequest = xmlInRequest.replace(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "");
xmlInRequest = xmlInRequest.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
JAXBContext jaxbContext = JAXBContext.newInstance("com.rmg.globalrates.adapter.models");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StreamSource streamSource = new StreamSource(new StringReader(xmlInRequest));
ObjectInfo objectInfo = (ObjectInfo) unmarshaller.unmarshal(streamSource);
System.out.println("----------------------------ObjectInfo-----------------------" + objectInfo.toString());
}
似乎CXF不支持RPC。 RPC,过去几天我所有困境的来源。
答案 1 :(得分:0)
我会尝试CXF消息格式和convertBodyTo = String。
我喜欢MESSAGE,因为它提供了更多的可见性和对响应和故障的控制。