2012-10-30 61 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信封?你能否提供一个文件上传的例子? –