我正在尝试理解如何将Apache Camel与任何提供WSDL的Web服务集成,以生成其类并随后调用其方法返回一些请求。
我已经学习了一些关于camel-spring-ws和camel-cxf包的知识。据我所见,Spring Web Services组件不支持使用WSDL,但CXF支持,但它仅支持连接到托管在CXF中的JAX-WS服务。
如果我从客户那里收到一个WSDL,我可以使用CXF吗?还是需要创建一个自定义组件来使用他的方法?
据我所见,实现它最简单的方法是创建一个进程或一个Bean来调用远程web服务。
我尝试实现一个生产者来调用远程Web服务。我的 beans.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:camel="http://camel.apache.org/schema/spring"
xmlns:cxf="http://camel.apache.org/schema/cxf"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<cxf:cxfEndpoint
id="osvEndpoint"
address="http://10.193.1.90:8767/"
serviceClass="siemens_hiq8000.SiemensHiq8000PortType"/>
<bean id="osvWebServiceProcessor" class="br.com.willianantunes.processor.OsvWebServiceProcessor" />
</beans>
我的路由:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:camel="http://camel.apache.org/schema/spring"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<routeContext id="osvWebServiceInvoke" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="quartz2://test?cron=0/10+*+*+*+*+?"/>
<process ref="osvWebServiceProcessor" />
<to uri="cxf:bean:osvEndpoint"/>
<to uri="log:live?level=INFO" />
</route>
</routeContext>
</beans>
而我的处理器:
public class OsvWebServiceProcessor implements Processor
{
@Override
public void process(Exchange exchange) throws Exception
{
Message inMessage = exchange.getIn();
// The method to be called
inMessage.setHeader(CxfConstants.OPERATION_NAME, "getVersion");
// Parameters to be passed into the web service
List<Object> params = new ArrayList<Object>();
ResultCodeStructHolder resultCodeStructHolder = new ResultCodeStructHolder();
VersionDataHolder versionDataHolder = new VersionDataHolder();
params.add(resultCodeStructHolder);
params.add(versionDataHolder);
inMessage.setBody(params);
}
}
方法getVersion需要以下参数:
public void getVersion(siemens_hiq8000.holders.ResultCodeStructHolder result,
siemens_hiq8000.holders.VersionDataHolder versionData) throws java.rmi.RemoteException;
我该如何通过它们呢?这些持有者必须用Web服务的响应填充。运行我的项目时,我收到以下错误:
[main] INFO org.apache.cxf.service.factory.ReflectionServiceFactoryBean - Creating Service {http://siemens_hiq8000/}SiemensHiq8000PortType from class siemens_hiq8000.SiemensHiq8000PortType
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.cxf.wsdl11.WSDLEndpointFactory.createEndpointInfo(Lorg/apache/cxf/service/model/ServiceInfo;Lorg/apache/cxf/service/model/BindingInfo;Ljava/util/List;)Lorg/apache/cxf/service/model/EndpointInfo;
at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpointInfo(AbstractWSDLBasedEndpointFactory.java:287)
at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:144)
at org.apache.cxf.frontend.ClientFactoryBean.create(ClientFactoryBean.java:91)
at org.apache.camel.component.cxf.CxfSpringEndpoint.createClient(CxfSpringEndpoint.java:116)
at org.apache.camel.component.cxf.CxfProducer.doStart(CxfProducer.java:76)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.impl.DefaultCamelContext.startService(DefaultCamelContext.java:2869)
at org.apache.camel.impl.DefaultCamelContext.doAddService(DefaultCamelContext.java:1097)
at org.apache.camel.impl.DefaultCamelContext.addService(DefaultCamelContext.java:1058)
at org.apache.camel.impl.ProducerCache.doGetProducer(ProducerCache.java:405)
at org.apache.camel.impl.ProducerCache.acquireProducer(ProducerCache.java:123)
at org.apache.camel.processor.SendProcessor.doStart(SendProcessor.java:219)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:74)
at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:59)
at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:103)
at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:89)
at org.apache.camel.processor.DelegateAsyncProcessor.doStart(DelegateAsyncProcessor.java:79)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:74)
at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:59)
at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:103)
at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:89)
at org.apache.camel.processor.RedeliveryErrorHandler.doStart(RedeliveryErrorHandler.java:1272)
at org.apache.camel.support.ChildServiceSupport.start(ChildServiceSupport.java:44)
at org.apache.camel.support.ChildServiceSupport.start(ChildServiceSupport.java:31)
at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:74)
at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:59)
at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:103)
at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:89)
at org.apache.camel.processor.interceptor.DefaultChannel.doStart(DefaultChannel.java:153)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:74)
at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:59)
at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:103)
at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:61)
at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:103)
at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:89)
at org.apache.camel.processor.MulticastProcessor.doStart(MulticastProcessor.java:1060)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:74)
at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:59)
at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:103)
at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:89)
at org.apache.camel.processor.DelegateAsyncProcessor.doStart(DelegateAsyncProcessor.java:79)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:74)
at org.apache.camel.impl.RouteService.startChildService(RouteService.java:340)
at org.apache.camel.impl.RouteService.warmUp(RouteService.java:182)
at org.apache.camel.impl.DefaultCamelContext.doWarmUpRoutes(DefaultCamelContext.java:3090)
at org.apache.camel.impl.DefaultCamelContext.safelyStartRouteServices(DefaultCamelContext.java:3020)
at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRoutes(DefaultCamelContext.java:2797)
at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:2653)
at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:167)
at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:2467)
at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:2463)
at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:2486)
at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:2463)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:2432)
at org.apache.camel.spring.SpringCamelContext.maybeStart(SpringCamelContext.java:255)
at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:121)
at org.apache.camel.spring.CamelContextFactoryBean.onApplicationEvent(CamelContextFactoryBean.java:332)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:331)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:773)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483)
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:216)
at org.apache.camel.spring.Main.doStart(Main.java:156)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.main.MainSupport.run(MainSupport.java:150)
at br.com.willianantunes.test.Program.main(Program.java:12)
解决方案
对于想让camel-cxf充当生产者的人,只需按照以下步骤进行操作:
- 首先,我下载了OpenScape Voice的最新WSDL文件here,该链接在developer page中提供。
- 客户端代码需要在选项serviceClass中通知SEI(服务端点接口)。您可以通过发出以下命令来生成:
wsdl2java -client -d "TargetFolderHere" -autoNameResolution "OpenScape-Voice_V8.00.28.01.wsdl"
- Insert the generated client classes in your project. The same applies to the WSDL file.
In your beans.xml inform the cxfEndpoint configuration.
<import resource="classpath:META-INF/cxf/cxf.xml" /> <cxf:cxfEndpoint xmlns:urn="urn:openscape-voice" id="osvEndpoint" address="http://10.193.1.90:8767/" serviceClass="voice.openscape.OpenscapeVoicePortType" wsdlURL="OpenScape-Voice_V8.00.28.01.wsdl" serviceName="urn:openscape_voice"> </cxf:cxfEndpoint> <bean id="osvWebServiceProcessor" class="br.com.willianantunes.processor.OsvWebServiceProcessor" />
Before the web service call, configure a processor to inform the parameters and the method to execute.
Message inMessage = exchange.getIn(); // The method to be called inMessage.setHeader(CxfConstants.OPERATION_NAME, "GetVersion"); // Parameters to be passed into the web service List<Object> params = new ArrayList<Object>(); Holder<ResultCodeStruct> result = new Holder<>(); Holder<VersionData> versionData = new Holder<>(); params.add(result); params.add(versionData); inMessage.setBody(params);
Now you can configure the route and insert in your camelContext as a ref to accomplish the task.
<routeContext id="osvWebServiceInvoke" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="quartz2://test?cron=0/10+*+*+*+*+?"/> <process ref="osvWebServiceProcessor"/> <to uri="cxf:bean:osvEndpoint"/> <to uri="log:live?level=INFO"/> </route> </routeContext>
而日志打印了以下内容:
- Quartz scheduler 'DefaultQuartzScheduler-main-application' initialized from an externally provided properties instance.
- Quartz scheduler version: 2.2.1
- Job Camel_main-application.test (triggerType=CronTriggerImpl, jobClass=CamelJob) is scheduled. Next fire date is Tue Mar 31 09:12:00 BRT 2015
- AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
- StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
- Creating Service {urn:openscape-voice}openscape_voice from WSDL: OpenScape-Voice_V8.00.28.01.wsdl
- Could not find endpoint/port for {urn:openscape-voice}openscape_voicePortTypePort in wsdl. Using {urn:openscape-voice}openscape_voice.
- Route: route1 started and consuming from: Endpoint[quartz2://test?cron=0%2F10+*+*+*+*+%3F]
- Starting scheduler.
- Scheduler DefaultQuartzScheduler-main-application_$_NON_CLUSTERED started.
- Total 1 routes, of which 1 is started.
- Apache Camel 2.15.0 (CamelContext: main-application) started in 10.759 seconds
- Exchange[ExchangePattern: InOnly, BodyType: org.apache.cxf.message.MessageContentsList, Body: [null, javax.xml.ws.Holder@508696f5, javax.xml.ws.Holder@333cf1ba]]
我在使用CXF时遇到了一些依赖问题,以下是我的pom.xml文件:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cxf</artifactId>
<version>${camel-version}</version>
</dependency>
<!-- Apache CXF -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-soap</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<!-- End of Apache CXF -->
你可以设计一个新的处理器或bean来获取来自Web服务的响应体消息(而不是纯SOAP消息,而是之前使用的POJO)。@Namphibian提供的答案很好,也可以满足我的目的。