首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用XML实现Camel cxfrs

用XML实现Camel cxfrs
EN

Stack Overflow用户
提问于 2014-09-19 13:38:31
回答 1查看 773关注 0票数 0

我试图访问的网站只发送xml。使用Camel的XML,我正在尝试获取该XML并通过cxrfs将其发送到另一个站点。现在,我可以让它变成一个bean,并且仅仅能够访问数据。

当我执行我的路线时,我不一定会有任何错误。它只是不对XML做任何事情,可能是因为它没有存储在身体中。不过不太确定。

输出

代码语言:javascript
复制
[INFO] Starting Camel ...
[pache.camel.spring.Main.main()] MainSupport                    INFO  Apache Camel 2.12.0.redhat-610379 starting
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  Apache Camel 2.12.0.redhat-610379 (CamelContext: camel) is starting
[pache.camel.spring.Main.main()] ManagedManagementStrategy      INFO  JMX is enabled
[pache.camel.spring.Main.main()] DefaultTypeConverter           INFO  Loaded 190 type converters
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  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.
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  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
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  Route: route1 started and consuming from: Endpoint[timer://foo?repeatCount=1]
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  Total 1 routes, of which 1 is started.
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  Apache Camel 2.12.0.redhat-610379 (CamelContext: camel) started in 0.335 seconds
[camel) thread #0 - timer://foo] BusApplicationContext          INFO  Refreshing org.apache.cxf.bus.spring.BusApplicationContext@3fd43e1d: startup date [Fri Sep 19 09:33:45 EDT 2014]; parent: org.springframework.context.support.FileSystemXmlApplicationContext@375d661b
[camel) thread #0 - timer://foo] idationXmlBeanDefinitionReader INFO  Loading XML bean definitions from class path resource [META-INF/cxf/cxf.xml]
[camel) thread #0 - timer://foo] idationXmlBeanDefinitionReader INFO  Loading XML bean definitions from class path resource [META-INF/cxf/camel/cxf-extension-camel.xml]
My Processor! Get In
null
My Processor! Get out
Message: [Body is null]

路由

代码语言:javascript
复制
<bean id="proc" class="com.b2bservicep1.httpserv.Proc"/>

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="timer://foo?repeatCount=1"/>
       <setHeader headerName="CamelHttpMethod">
            <constant>GET</constant>
        </setHeader>
        <to uri="cxfrs://http://myxmlsitethatimcalling.com?resourceClasses=com.b2bservicep1.httpserv.Proc"/>
        <to uri="bean:proc?method=process"/>
    </route>
</camelContext>

</beans>

我写的处理器

代码语言:javascript
复制
package com.b2bservicep1.httpserv;

import javax.ws.rs.GET;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

public class Proc implements Processor{

    @Override @GET
    public void process(Exchange arg0) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("My Processor! Get In");
        System.out.println(arg0.getIn().getBody());
        System.out.println("My Processor! Get out");
        System.out.println(arg0.getOut());
    }
EN

回答 1

Stack Overflow用户

发布于 2014-09-28 01:53:01

您需要指定多个方法头。下面是一个例子

代码语言:javascript
复制
@Test
public void testGetCustomerWithCxfRsEndpoint() {
    Exchange exchange 
        = template.send("cxfrs://http://localhost:" + getPort1() + "/" + getClass().getSimpleName() + "/?httpClientAPI=true", new Processor() {
            public void process(Exchange exchange) throws Exception {
                exchange.setPattern(ExchangePattern.InOut);
                Message inMessage = exchange.getIn();
                // set the Http method
                inMessage.setHeader(Exchange.HTTP_METHOD, "GET");
                // set the relative path
                inMessage.setHeader(Exchange.HTTP_PATH, "/customerservice/customers/123");                
                // Specify the response class , cxfrs will use InputStream as the response object type 
                inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, Customer.class);
                // since we use the Get method, so we don't need to set the message body
                inMessage.setBody(null);                
            }
        });

    // get the response message 
    Customer response = (Customer) exchange.getOut().getBody();
    assertNotNull("The response should not be null ", response);
    assertEquals("Get a wrong customer id ", String.valueOf(response.getId()), "123");
    assertEquals("Get a wrong customer name", response.getName(), "John");
    assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
}

您可以在这个单元测试中找到有关如何使用camel-cxfrs生产者的更多信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25935323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档