CXFRSPage edited by willem jiangCAMEL-1909CXFRS ComponentThe cxfrs: component provides integration with Apache CXF for connecting to JAX-RS services hosted in CXF. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-cxf</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>
You can also configure the CXF REST endpoint through the spring configuration. Since there are lots of difference between the CXF REST client and CXF REST Server, we provides different configuration for them. How to configure the REST endpoint in Camel ?In camel-cxf schema file, there are two elements for the REST endpoint definition. cxf:rsServer for REST consumer, cxf:rsClient for REST producer. <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" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd "> <!-- Defined the real JAXRS back end service --> <jaxrs:server id="restService" address="http://localhost:9002" staticSubresourceResolution="true"> <jaxrs:serviceBeans> <ref bean="customerService"/> </jaxrs:serviceBeans> </jaxrs:server> <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.JSONProvider"/> <bean id="customerService" class="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService" /> <!-- Defined the server endpoint to create the cxf-rs consumer --> <cxf:rsServer id="rsServer" address="http://localhost:9000" serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService" /> <!-- Defined the client endpoint to create the cxf-rs consumer --> <cxf:rsClient id="rsClient" address="http://localhost:9002" serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"/> <!-- The camel route context --> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="cxfrs://bean://rsServer"/> <!-- We can remove this configure as the CXFRS producer is using the HttpAPI by default --> <setHeader headerName="CamelCxfRsUsingHttpAPI"> <constant>True</constant> </setHeader> <to uri="cxfrs://bean://rsClient"/> </route> </camelContext> </beans> How to consumer the REST request in Camel ?CXF JAXRS front end implements the JAXRS(JSR311) API, so we can export the resources classes as a REST service. And we leverage the CXF Invoker API to turn a REST request into a normal Java object method invocation. Here is an example protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() { errorHandler(new NoErrorHandlerBuilder()); from(CXF_RS_ENDPOINT_URI).process(new Processor() { public void process(Exchange exchange) throws Exception { Message inMessage = exchange.getIn(); // Get the operation name from in message String operationName = inMessage.getHeader(CxfConstants.OPERATION_NAME, String.class); // The parameter of the invocation is stored in the body of in message String id = (String) inMessage.getBody(Object[].class)[0]; if ("getCustomer".equals(operationName)) { String httpMethod = inMessage.getHeader(Exchange.HTTP_METHOD, String.class); assertEquals("Get a wrong http method", "GET", httpMethod); String uri = inMessage.getHeader(Exchange.HTTP_URI, String.class); if ("/customerservice/customers/126".equals(uri)) { Customer customer = new Customer(); customer.setId(Long.parseLong(id)); customer.setName("Willem"); // We just put the response Object into the out message body exchange.getOut().setBody(customer); } else { Response r = Response.status(404).entity("Can't found the customer with uri " + uri).build(); throw new WebApplicationException(r); } } } }); } }; } How to invoke the REST service through camel-cxfrs producer ?CXF JAXRS front end implements a proxy based client API, with this API you can invoke the remote REST service through a proxy. Here is an example Exchange exchange = template.send("direct://proxy", new Processor() { public void process(Exchange exchange) throws Exception { exchange.setPattern(ExchangePattern.InOut); Message inMessage = exchange.getIn(); // set the operation name inMessage.setHeader(CxfConstants.OPERATION_NAME, "getCustomer"); // using the proxy client API inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.FALSE); // set the parameters , if you just have one parameter // camel will put this object into an Object[] itself inMessage.setBody("123"); } }); // 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"); CXF JAXRS front end also provides a http centric client API, You can also invoke this API from camel-cxfrs producer. You need to specify the HTTP_PATH and Http method and let the the producer know to use the http centric client by using the URI option httpClientAPI or set the message header with CxfConstants.CAMEL_CXF_RS_USING_HTTP_API. You can turn the response object to the type class that you specify with CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS. Exchange exchange = template.send("direct://http", new Processor() { public void process(Exchange exchange) throws Exception { exchange.setPattern(ExchangePattern.InOut); Message inMessage = exchange.getIn(); // using the http central client API inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.TRUE); // 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"); From Camel 2.1, we also support to specify the query parameters from cxfrs URI for the CXFRS http centric client. Exchange exchange = template.send("cxfrs://http://localhost:9003/testQuery?httpClientAPI=true&q1=12&q2=13"
To support the Dynamical routing, you can override the URI's query parameters by using the CxfConstants.CAMEL_CXF_RS_QUERY_MAP header to set the parameter map for it.To support the Dynamical routing, you can override the URI's query parameters by using the CxfConstants.CAMEL_CXF_RS_QUERY_MAP header to set the parameter map for it. Map<String, String> queryMap = new LinkedHashMap<String, String>(); queryMap.put("q1", "new"); queryMap.put("q2", "world"); inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_QUERY_MAP, queryMap);
Change Notification Preferences
View Online
|
View Change
|
Add Comment
|
- [CONF] Apache Camel > CXFRS confluence
- [CONF] Apache Camel > CXFRS confluence
- [CONF] Apache Camel > CXFRS confluence
- [CONF] Apache Camel > CXFRS confluence