如何使用Camel将本地服务的Rest请求路由到远程请求

我试图从一个cxfrest服务路由rest请求到另一个。 我看了一下http://camel.apache.org/cxfrs.html ,这有助于理解过程的一部分。 我在remoteService调用级别有一个classCastException。

我究竟做错了什么 ?

<cxf:rsServer id="exposedService" address="/exposed/" serviceClass="com.example.project.ExposedService" /> <!-- using different classes since we are not just proxying --> <cxf:rsClient id="remoteService" address="http://remote.com/service" serviceClass="com.example.project.RemoteService" /> <camel:camelContext xmlns="http://camel.apache.org/schema/spring"> <package>com.example.project</package> </camel:camelContext> 

服务类别:

 @Path("/myservice") public class ExposedService { @POST @Consumes("application/xml") public void postResource(javax.xml.transform.Source resource) { // source : not using jaxb just plain xml // only serves to configure the endpoint (?) } } public class RemoteService { @POST @Consumes("application/xml") public void postResource(javax.xml.transform.Source resource) { // source : not using jaxb just plain xml // only serves to configure the endpoint (?) } } 

RouteBuilder:

 public class MyRouteBuilder extends RouteBuilder { public void configure() { from("cxfrs://bean://exposedService") .process(...) // a few System.out.println's .to("cxfrs://bean://remoteService") ; } } 

堆栈跟踪:

 | 错误|  qtp-2045010446-1 |  DefaultErrorHandler |  68  -  org.apache.camel.camel-core  -  2.6.0 |  exchangeId发送失败:ID-Lab-local-52879-1305561896560-7-2。 交付尝试后耗尽:1已捕获:java.lang.ClassCastException
 java.lang.ClassCastException
        在java.lang.Class.cast(Class.java:2990)[:1.6.0_24]
        在org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:329)[116:org.apache.cxf.bundle:2.3.2]
        在org.apache.camel.component.cxf.jaxrs.CxfRsProducer.invokeHttpClient(CxfRsProducer.java:147)[118:org.apache.camel.camel-cxf:2.6.0]
        在org.apache.camel.component.cxf.jaxrs.CxfRsProducer.process(CxfRsProducer.java:77)[118:org.apache.camel.camel-cxf:2.6.0]
        在org.apache.camel.impl.converter.AsyncProcessorTypeConverter $ ProcessorToAsyncProcessorBridge.process(AsyncProcessorTypeConverter.java:50)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:70)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.processor.SendProcessor $ 2.doInAsyncProducer(SendProcessor.java:104)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.impl.ProducerCache.doInAsyncProducer(ProducerCache.java:272)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.processor.SendProcessor.process(SendProcessor.java:98)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:70)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:98)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:89)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:68)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:70)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:98)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:89)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.processor.interceptor.TraceInterceptor.process(TraceInterceptor.java:174)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:70)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.processor.RedeliveryErrorHandler.processErrorHandler(RedeliveryErrorHandler.java:299)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:208)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.processor.DefaultChannel.process(DefaultChannel.java:269)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:70)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.processor.Pipeline.process(Pipeline.java:125)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.processor.Pipeline.process(Pipeline.java:80)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.processor.UnitOfWorkProcessor.process(UnitOfWorkProcessor.java:102)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:70)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:98)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:89)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:68)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:91)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:85)[68:org.apache.camel.camel-core:2.6.0]
        在org.apache.camel.component.cxf.jaxrs.CxfRsInvoker.syncInvoke(CxfRsInvoker.java:134)[118:org.apache.camel.camel-cxf:2.6.0]
        在org.apache.camel.component.cxf.jaxrs.CxfRsInvoker.performInvocation(CxfRsInvoker.java:68)[118:org.apache.camel.camel-cxf:2.6.0]
        在org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:89)[116:org.apache.cxf.bundle:2.3.2]
        在org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:153)[116:org.apache.cxf.bundle:2.3.2]
        在org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:87)[116:org.apache.cxf.bundle:2.3.2]
        在org.apache.cxf.interceptor.ServiceInvokerInterceptor $ 1.run(ServiceInvokerInterceptor.java:58)[116:org.apache.cxf.bundle:2.3.2]
        在java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:441)[:1.6.0_24]
        在java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:303)[:1.6.0_24]
        在java.util.concurrent.FutureTask.run(FutureTask.java:138)[:1.6.0_24]
        在org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)[116:org.apache.cxf.bundle:2.3.2]
        在org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:106)[116:org.apache.cxf.bundle:2.3.2]
        在org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255)[116:org.apache.cxf.bundle:2.3.2]
        在org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:113)[116:org.apache.cxf.bundle:2.3.2]
        在org.apache.cxf.transport.http_osgi.OsgiDestination.doMessage(OsgiDestination.java:79)[116:org.apache.cxf.bundle:2.3.2]
        在org.apache.cxf.transport.http_osgi.OsgiServletController.invokeDestination(OsgiServletController.java:336)[116:org.apache.cxf.bundle:2.3.2]
        在org.apache.cxf.transport.http_osgi.OsgiServletController.invoke(OsgiServletController.java:108)[116:org.apache.cxf.bundle:2.3.2]
        在org.apache.cxf.transport.http_osgi.OsgiServlet.invoke(OsgiServlet.java:53)[116:org.apache.cxf.bundle:2.3.2]
        在org.apache.cxf.transport.http_osgi.SpringOsgiServlet.invoke(SpringOsgiServlet.java:48)[116:org.apache.cxf.bundle:2.3.2]
        在org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179)[116:org.apache.cxf.bundle:2.3.2]
        在org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:103)[116:org.apache.cxf.bundle:2.3.2]
        在javax.servlet.http.HttpServlet.service(HttpServlet.java:595)[93:org.apache.geronimo.specs.geronimo-servlet_3.0_spec:1.0]
        在org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159)[116:org.apache.cxf.bundle:2.3.2]
        在org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        在org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        在org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.handle(HttpServiceServletHandler.java:64)[133:org.ops4j.pax.web.pax-web-jetty:0.7.4]
        在org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        在org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        在org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.handle(HttpServiceContext.java:111)[133:org.ops4j.pax.web.pax-web-jetty:0.7.4]
        在org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:68)[133:org.ops4j.pax.web.pax-web-jetty:0.7.4]
        在org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        在org.mortbay.jetty.Server.handle(Server.java:326)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        在org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        在org.mortbay.jetty.HttpConnection $ RequestHandler.content(HttpConnection.java:943)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
         at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        在org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        在org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        在org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        在org.mortbay.thread.QueuedThreadPool $ PoolThread.run(QueuedThreadPool.java:582)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]

有一个在这里做几乎相同的事情的工作的例子…

http://camel.apache.org/cxf-proxy-example.html

从此开始,然后逐个放弃您的更改,并查看它在哪里破坏…