如何使用 Apache camel 使用 cxf 组件调用第三方 Web 服务 [英] How to call a third party webservice using Apache camel using cxf componnet

查看:26
本文介绍了如何使用 Apache camel 使用 cxf 组件调用第三方 Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了 Apache camel 网站和 camel in action 上的示例,它们都非常关注公开 Web 服务.我在网上找到的唯一示例不起作用.

I went through the example on Apache camel website and camel in action and both focus heavily on exposing a webservice. The only example I found online does not work.

我正在尝试访问第三方网络服务 wsdl - http://www.webservicex.net/CurrencyConvertor.asmx?WSDL

I am trying to access a third party webservice wsdl - http://www.webservicex.net/CurrencyConvertor.asmx?WSDL

此服务具有计算货币汇率的操作/方法.操作名称为ConversionRate".

This service has a operation/method that calculates the currency rate. The operation name is "ConversionRate".

我这样定义路线 -

      <?xml version="1.0" encoding="UTF-8"?>
<!-- Configures the Camel Context-->

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


  <camelContext xmlns="http://camel.apache.org/schema/spring">

    <route>
        <description>here is a sample which processes the input files
         (leaving them in place - see the 'noop' flag)
         then performs content based routing on the message using XPath</description>
        <from uri="file:src/data/order?noop=true"/>
        <log message="${body}"/>

        <to uri="cxf://http://www.webservicex.net/CurrencyConvertor.asmx?wsdlURL=http://www.webservicex.net/CurrencyConvertor.asmx?wsdl&amp;serviceName=CurrencyConvertor&amp;portName=http://www.webserviceX.NET/tns:CurrencyConvertorSoap&amp;dataFormat=MESSAGE"/>
        <log message="${body}"/>
    </route>
</camelContext>

</beans>

我已将示例有效负载 xml 定义为 -

I have defined the sample payload xml as -

<order>
        <Id>1</Id>
        <Price>10.00</Price>
        <Quantity>2</Quantity>
</order>

我在运行此示例时收到此错误.

I am getting this error when I run this example.

   Error occurred while running main from: org.apache.camel.spring.Main
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.camel.maven.RunMojo$1.run(RunMojo.java:440)
    at java.lang.Thread.run(Thread.java:744)
Caused by: org.apache.camel.FailedToCreateProducerException: Failed to create Producer for endpoint: Endpoint[cxf://http://www.webservicex.net/CurrencyConvertor.asmx?dataFormat=MESSAGE&portName=http%3A%2F%2Fwww.webserviceX.NET%2Ftns%3ACurrencyConvertorSoap&serviceName=CurrencyConvertor&wsdlURL=http%3A%2F%2Fwww.webservicex.net%2FCurrencyConvertor.asmx%3Fwsdl]. Reason: org.apache.cxf.service.factory.ServiceConstructionException: Could not find definition for service CurrencyConvertor.
    at org.apache.camel.impl.ProducerCache.doGetProducer(ProducerCache.java:395)
    at org.apache.camel.impl.ProducerCache.acquireProducer(ProducerCache.java:114)
    at org.apache.camel.impl.ProducerCache.startProducer(ProducerCache.java:145)
    at org.apache.camel.processor.SendProcessor.doStart(SendProcessor.java:175)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:62)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:52)
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:73)
    at org.apache.camel.processor.DelegateAsyncProcessor.doStart(DelegateAsyncProcessor.java:78)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:62)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:52)
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:73)
    at org.apache.camel.processor.DelegateAsyncProcessor.doStart(DelegateAsyncProcessor.java:78)
    at org.apache.camel.processor.interceptor.TraceInterceptor.doStart(TraceInterceptor.java:358)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:62)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:52)
    at org.apache.camel.processor.WrapProcessor.doStart(WrapProcessor.java:52)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:62)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:52)
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:73)
    at org.apache.camel.processor.RedeliveryErrorHandler.doStart(RedeliveryErrorHandler.java:1064)
    at org.apache.camel.support.ChildServiceSupport.start(ChildServiceSupport.java:41)
    at org.apache.camel.support.ChildServiceSupport.start(ChildServiceSupport.java:28)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:62)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:52)
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:73)
    at org.apache.camel.processor.interceptor.DefaultChannel.doStart(DefaultChannel.java:153)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:62)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:52)
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:85)
    at org.apache.camel.processor.MulticastProcessor.doStart(MulticastProcessor.java:938)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:62)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:52)
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:73)
    at org.apache.camel.processor.DelegateAsyncProcessor.doStart(DelegateAsyncProcessor.java:78)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:62)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:52)
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:73)
    at org.apache.camel.processor.DelegateAsyncProcessor.doStart(DelegateAsyncProcessor.java:78)
    at org.apache.camel.processor.UnitOfWorkProcessor.doStart(UnitOfWorkProcessor.java:88)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:62)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:52)
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:73)
    at org.apache.camel.processor.DelegateAsyncProcessor.doStart(DelegateAsyncProcessor.java:78)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:62)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:52)
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:73)
    at org.apache.camel.processor.DelegateAsyncProcessor.doStart(DelegateAsyncProcessor.java:78)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:62)
    at org.apache.camel.impl.RouteService.startChildService(RouteService.java:322)
    at org.apache.camel.impl.RouteService.warmUp(RouteService.java:151)
    at org.apache.camel.impl.DefaultCamelContext.doWarmUpRoutes(DefaultCamelContext.java:2000)
    at org.apache.camel.impl.DefaultCamelContext.safelyStartRouteServices(DefaultCamelContext.java:1928)
    at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRoutes(DefaultCamelContext.java:1716)
    at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:1597)
    at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:1453)
    at org.apache.camel.spring.SpringCamelContext.doStart(SpringCamelContext.java:179)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)
    at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:1421)
    at org.apache.camel.spring.SpringCamelContext.maybeStart(SpringCamelContext.java:228)
    at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:118)
    at org.apache.camel.spring.CamelContextFactoryBean.onApplicationEvent(CamelContextFactoryBean.java:283)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:327)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:941)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:475)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    at org.apache.camel.spring.Main.createDefaultApplicationContext(Main.java:186)
    at org.apache.camel.spring.Main.doStart(Main.java:140)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)
    at org.apache.camel.main.MainSupport.run(MainSupport.java:141)
    at org.apache.camel.main.MainSupport.run(MainSupport.java:332)
    at org.apache.camel.spring.Main.main(Main.java:73)
    ... 6 more
Caused by: org.apache.cxf.service.factory.ServiceConstructionException: Could not find definition for service CurrencyConvertor.
    at org.apache.cxf.wsdl11.WSDLServiceFactory.create(WSDLServiceFactory.java:139)
    at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.buildServiceFromWSDL(ReflectionServiceFactoryBean.java:412)
    at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean.java:534)
    at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:248)
    at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:101)
    at org.apache.cxf.frontend.ClientFactoryBean.create(ClientFactoryBean.java:90)
    at org.apache.camel.component.cxf.CxfEndpoint.createClient(CxfEndpoint.java:527)
    at org.apache.camel.component.cxf.CxfProducer.doStart(CxfProducer.java:76)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:62)
    at org.apache.camel.impl.ProducerCache.doGetProducer(ProducerCache.java:393)
    ... 88 more
*************************************.

推荐答案

根据 CXF 文档.

serviceNameportName 的格式应该是 {namespace}name

The format for serviceName and portName should be {namespace}name

尝试:

  • serviceName={http://www.webserviceX.NET/}CurrencyConvertor
  • portName={http://www.webserviceX.NET/}CurrencyConvertorSoap

这篇关于如何使用 Apache camel 使用 cxf 组件调用第三方 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆