2016-04-13 25 views
0

我正面临将SOAP请求从CXF有效负载格式转换为所需格式的问题。CxfPayload对象不会传播SOAP请求中的值

这是要求是什么样子:

<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 /文字风格的wsdl。

问题是,当来自exchange的请求被转换为CxfPayload。它变为空。该DOMSource的样子:

<ObjectInfo> 
    <value1>null</value1> 
    <value2>null</value2> 
</ObjectInfo> 

我的SOAP请求实际上包含ObjectInfo后夫妇更多的元素(WSDL中具有特定的SOAP请求多部分消息),这同样为空。

+0

我怀疑你的问题可能与CXF不支持RPC风格的Web服务有关。看到这个:http://stackoverflow.com/questions/14831499/will-apache-cxf-supports-jax-rpc-based-web-servicessoap。 – Namphibian

+0

啊。我也怀疑过。值得一试。同时,我找到了一个解决方法,而不是使用Source,我使用XmlConverter将主体转换为String。然后将它解组到所需的对象类型。 –

回答

0

我发现了一个临时解决我的问题。

而不是使用源,我使用XmlConverter将正文转换为字符串。然后将它解组到所需的对象类型。

@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()); 
} 

看起来好像RPC不被CXF支持。 RPC,这是我过去几天所有困难的根源。

+0

这用作解决方法。不完整。只有适当的解决方案是将WSDL从RPC样式修改为Document Style –

0

我会尝试CXF消息格式和convertBodyTo =字符串进一步。

我喜欢MESSAGE,因为它提供了更多的可见性和对响应和故障的控制。

+0

是的。事实证明,WSDL格式不正确,我无法改变它。所以我公开了一个休息端点,它将接受传入的请求作为XML。我认为现在是最好的选择。 –