2012-10-30 141 views
1

我想從駱駝呼叫web服務。但每次我致電該服務時,我都會收到空。 你能幫我找到解決方案嗎?從駱駝呼叫web服務

服務在tomcat上運行,我可以用soapUI測試它。這是來自SoapUI的請求。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:hel="http://helloworld.localhost"> 
    <soapenv:Header/> 
     <soapenv:Body> 
      <hel:HelloWorldRequest> 
       <hel:input>Pavel</hel:input> 
      </hel:HelloWorldRequest> 
     </soapenv:Body> 
    </soapenv:Envelope> 

並且響應返回Hello Pavel。 我遵循CamelInAction指南創建合同優先Web服務。 我能夠運行讀取文件並將其發送到Web服務的路由。

該路線的代碼如下。

public class FileToWsRoute extends RouteBuilder { 
public void configure() { 
    from("file://src/data?noop=false") 
    .process(new FileProcessor()) 
    .to("cxf:bean:helloWorld"); 
    } 

} 

的FileProcessor類看起來像這樣:

public class FileProcessor implements Processor { 

public void process(Exchange exchange) throws Exception { 

    System.out.println("We just downloaded: " 
      + exchange.getIn().getHeader("CamelFileName")); 
    String text = 
    "<?xml version='1.0' ?>" 
    +"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:hel=\"http://helloworld.localhost\">" 
    +"<soapenv:Header/>" 
    + "<soapenv:Body>" 
    +  " <hel:HelloWorldRequest>" 
    +  " <hel:input>WhatsUP</hel:input>" 
    +  " </hel:HelloWorldRequest>" 
    + "</soapenv:Body>" 
    +"</soapenv:Envelope>"; 

    exchange.getIn().setBody(text); 
} 
} 

在下一版本我想生成通過CXF-CODEGEN-插件(HalloWorld.java,HelloWorldImpl.java生成的對象的請求, HelloWorldRequest.java,HelloWorldResponse.java,HelloWorldService.java,ObjectFactory.java,package-info.java)。

在駱駝cxf.xml我:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:cxf="http://camel.apache.org/schema/cxf" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://camel.apache.org/schema/cxf 
    http://camel.apache.org/schema/cxf/camel-cxf.xsd"> 

    <import resource="classpath:META-INF/cxf/cxf.xml"/> 
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> 
    <import resource="classpath:META-INF/cxf/cxf-extension-http-jetty.xml"/> 

    <cxf:cxfEndpoint id="helloWorld" 
       address="http://localhost:8080/ode/processes/HelloWorld" 
       serviceClass="localhost.helloworld.HelloWorld" 
       wsdlURL="wsdl/HelloWorld.wsdl"/> 
</beans> 

要讀我使用這條路線Web服務響應。

public class WsToQueueRoute extends RouteBuilder { 
    public void configure() { 
    from("cxf:bean:helloWorld") 
    .to("seda:incomingOrders") 
    .transform().constant("OK"); 
    } 

} 

最後一條路由從SEDA獲取數據...

public class QueueToProcessRoute extends RouteBuilder { 
public void configure() { 
    from("seda:incomingOrders") 
    .process(new PrintResult()); 
    } 

} 

...並打印出結果。

public class PrintResult implements Processor { 

public void process(Exchange exchange) throws Exception { 

    System.out.println("Data received: " 
      + exchange.getIn().getBody(String.class)); 
} 

} 

從執行的輸出是: 收到的數據:空

我希望我可以用CXF對象分析一些XML文件。你能幫我找到問題嗎?

謝謝

帕維爾

回答

0

的問題可能與發送到您的process方法的數據格式。我會嘗試是增加camel cxf屬性,這樣的端點:

<cxf:properties> 
    <entry key="dataFormat" value="POJO"/> 
</cxf:properties> 

通過改變DATAFORMAT到POJO您會收到你的消息。所以,你的camel cxf終點應該是這樣的:

<cxf:cxfEndpoint id="helloWorld" 
       address="http://localhost:8080/ode/processes/HelloWorld" 
       serviceClass="localhost.helloworld.HelloWorld" 
       wsdlURL="wsdl/HelloWorld.wsdl"> 
    <cxf:properties> 
     <entry key="dataFormat" value="POJO"/> 
    </cxf:properties> 
    </cxf:cxfEndpoint> 

我有類似的問題一次,這解決了我的問題,但我是用駱駝都在Spring不是Java類。

如果這不起作用嘗試添加參數:?dataFormat=POJO到您的路線是這樣的:

from("cxf:bean:helloWorld?dataFormat=POJO") 
+0

謝謝你的回答。這不是問題,但另一方面它不會造成任何傷害。看到我的工作示例波紋管。 –

+0

任何人都可以請告訴我如何運行這個例子? –

3

用這個例子的問題是在課堂上FileProcessor和PrintResult。我也簡化了這個例子,所以我現在只使用一個路徑FileToWsRoute。

public class FileToWsRoute extends RouteBuilder { 
public void configure() { 
    from("file://src/data?noop=true") 
    .process(new FileProcessor()) 
    .to("cxf:bean:helloWorld") 
    .process(new PrintResult()); 
    } 
} 

FileProcessor已更改爲此。

public class FileProcessor implements Processor { 

public void process(Exchange exchange) throws Exception { 
     HelloWorldRequest hs = new HelloWorldRequest(); 
     hs.setInput("Pavel"); 
     exchange.getOut().setBody(hs); 
    } 
} 

PrintProcessor已更改爲此。

public class PrintResult implements Processor { 
public void process(Exchange exchange) throws Exception { 
    MessageContentsList msgList = (MessageContentsList) exchange.getIn().getBody(); 
    HelloWorldResponse resp = (HelloWorldResponse) msgList.get(0); 
    String result = resp.getResult();  
     System.out.println("Data received: " + result); 
    } 

} 

我認爲這是一個很好的例子,爲其他誰與駱駝和web服務鬥爭。

+0

可能是一個好主意,作爲github或bitbucket等的示例項目共享此代碼。 – irfn

+0

@ mr.pohl這個HelloWorldRequest是什麼?處理器還是隻是一個SOAP信封?你能否提供一個文件上傳的例子? –