This is an automated email from the ASF dual-hosted git repository. ningjiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 39c0d63 Updated the code snipet for camel-cxfrs component 39c0d63 is described below commit 39c0d63d923bfe9236834ecb1c4470bb7e9e7eaa Author: Willem Jiang <jiangni...@huawei.com> AuthorDate: Sun May 27 15:23:11 2018 +0800 Updated the code snipet for camel-cxfrs component --- .../camel-cxf/src/main/docs/cxfrs-component.adoc | 143 ++++++++++++++++++--- 1 file changed, 128 insertions(+), 15 deletions(-) diff --git a/components/camel-cxf/src/main/docs/cxfrs-component.adoc b/components/camel-cxf/src/main/docs/cxfrs-component.adoc index 31af57c..1307bde 100644 --- a/components/camel-cxf/src/main/docs/cxfrs-component.adoc +++ b/components/camel-cxf/src/main/docs/cxfrs-component.adoc @@ -244,21 +244,21 @@ Given a JAX-RS resource class with this method: [source,java] ------------------------------------------------------------------------------------------------------------------------------------------------ - @POST @Path("/customers/{type}") - public Response newCustomer(Customer customer, @PathParam("type") String type, @QueryParam("active") @DefaultValue("true") boolean active) { - return null; - } +@POST @Path("/customers/{type}") +public Response newCustomer(Customer customer, @PathParam("type") String type, @QueryParam("active") @DefaultValue("true") boolean active) { + return null; +} ------------------------------------------------------------------------------------------------------------------------------------------------ Serviced by the following route: [source,java] -------------------------------------------------------------------------------------------- - from("cxfrs:bean:rsServer?bindingStyle=SimpleConsumer") - .recipientList(simple("direct:${header.operationName}")); +from("cxfrs:bean:rsServer?bindingStyle=SimpleConsumer") + .recipientList(simple("direct:${header.operationName}")); - from("direct:newCustomer") - .log("Request: type=${header.type}, active=${header.active}, customerData=${body}"); +from("direct:newCustomer") + .log("Request: type=${header.type}, active=${header.active}, customerData=${body}"); -------------------------------------------------------------------------------------------- The following HTTP request with XML payload (given that the Customer DTO @@ -294,7 +294,7 @@ implements the https://javaee.github.io/jsr311/[JAX-RS (JSR-311) API], so we can export the resources classes as a REST service. And we leverage the http://cxf.apache.org/docs/invokers.html[CXF Invoker API] to turn a REST request into a normal Java object method -invocation. + +invocation. Unlike the <<restlet-component,Camel Restlet>> component, you don't need to specify the URI template within your endpoint, CXF takes care of the REST request URI to resource class method mapping according to the @@ -302,6 +302,39 @@ JSR-311 specification. All you need to do in Camel is delegate this method request to a right processor or endpoint. Here is an example of a CXFRS route... +[source,java] +---- +private static final String CXF_RS_ENDPOINT_URI = + "cxfrs://http://localhost:" + CXT + "/rest?resourceClasses=org.apache.camel.component.cxf.jaxrs.testbean.CustomerServiceResource"; +private static final String CXF_RS_ENDPOINT_URI2 = + "cxfrs://http://localhost:" + CXT + "/rest2?resourceClasses=org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"; +private static final String CXF_RS_ENDPOINT_URI3 = + "cxfrs://http://localhost:" + CXT + "/rest3?" + + "resourceClasses=org.apache.camel.component.cxf.jaxrs.testbean.CustomerServiceNoAnnotations&" + + "modelRef=classpath:/org/apache/camel/component/cxf/jaxrs/CustomerServiceModel.xml"; +private static final String CXF_RS_ENDPOINT_URI4 = + "cxfrs://http://localhost:" + CXT + "/rest4?" + + "modelRef=classpath:/org/apache/camel/component/cxf/jaxrs/CustomerServiceDefaultHandlerModel.xml"; +private static final String CXF_RS_ENDPOINT_URI5 = + "cxfrs://http://localhost:" + CXT + "/rest5?" + + "propagateContexts=true&" + + "modelRef=classpath:/org/apache/camel/component/cxf/jaxrs/CustomerServiceDefaultHandlerModel.xml"; +protected RouteBuilder createRouteBuilder() throws Exception { + final Processor testProcessor = new TestProcessor(); + final Processor testProcessor2 = new TestProcessor2(); + final Processor testProcessor3 = new TestProcessor3(); + return new RouteBuilder() { + public void configure() { + errorHandler(new NoErrorHandlerBuilder()); + from(CXF_RS_ENDPOINT_URI).process(testProcessor); + from(CXF_RS_ENDPOINT_URI2).process(testProcessor); + from(CXF_RS_ENDPOINT_URI3).process(testProcessor); + from(CXF_RS_ENDPOINT_URI4).process(testProcessor2); + from(CXF_RS_ENDPOINT_URI5).process(testProcessor3); + } + }; +} +---- And the corresponding resource class used to configure the endpoint... @@ -322,6 +355,30 @@ set on the Camel exchange and the route execution will continue as usual. This can be useful for integrating the existing JAX-RS implementations into Camel routes and for post-processing JAX-RS Responses in custom processors. +[source,java] +---- +@Path("/customerservice/") +public interface CustomerServiceResource { + + @GET + @Path("/customers/{id}/") + Customer getCustomer(@PathParam("id") String id); + + @PUT + @Path("/customers/") + Response updateCustomer(Customer customer); + + @Path("/{id}") + @PUT() + @Consumes({ "application/xml", "text/plain", + "application/json" }) + @Produces({ "application/xml", "text/plain", + "application/json" }) + Object invoke(@PathParam("id") String id, + String payload); +} +---- + ### How to invoke the REST service through camel-cxfrs producer The http://cxf.apache.org/docs/jax-rs.html[CXF JAXRS front end] @@ -330,12 +387,41 @@ http://cxf.apache.org/docs/jax-rs-client-api.html#JAX-RSClientAPI-Proxy-basedAPI proxy-based client API], with this API you can invoke the remote REST service through a proxy. The `camel-cxfrs` producer is based on this http://cxf.apache.org/docs/jax-rs-client-api.html#JAX-RSClientAPI-Proxy-basedAPI[proxy -API]. + +API]. You just need to specify the operation name in the message header and prepare the parameter in the message body, the camel-cxfrs producer will generate right REST request for you. Here is an example: +[source,java] +---- +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 a customer header + inMessage.setHeader("key", "value"); + // setup the accept content type + inMessage.setHeader(Exchange.ACCEPT_CONTENT_TYPE, "application/json"); + // 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 ", 123, response.getId()); +assertEquals("Get a wrong customer name", "John", response.getName()); +assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); +assertEquals("Get a wrong header value", "value", exchange.getOut().getHeader("key")); +---- The http://cxf.apache.org/docs/jax-rs.html[CXF JAXRS front end] also provides @@ -351,13 +437,40 @@ http://camel.apache.org/maven/current/camel-cxf/apidocs/org/apache/camel/compone You can turn the response object to the type class specified with the message header http://camel.apache.org/maven/current/camel-cxf/apidocs/org/apache/camel/component/cxf/CxfConstants.html#CAMEL_CXF_RS_RESPONSE_CLASS[CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS]. - +[source,java] +---- +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); + // set a customer header + inMessage.setHeader("key", "value"); + // since we use the Get method, so we don't need to set the message body + inMessage.setBody(null); + } +}); +---- From Camel 2.1, we also support to specify the query parameters from cxfrs URI for the CXFRS http centric client. - -Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: -Index: 20, Size: 20 - +[source,java] +---- +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 http://camel.apache.org/maven/current/camel-cxf/apidocs/org/apache/camel/component/cxf/CxfConstants.html#CAMEL_CXF_RS_QUERY_MAP[CxfConstants.CAMEL_CXF_RS_QUERY_MAP] header to set the parameter map for it. +[source,java] +---- +Map<String, String> queryMap = new LinkedHashMap<>(); +queryMap.put("q1", "new"); +queryMap.put("q2", "world"); +inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_QUERY_MAP, queryMap); +---- -- To stop receiving notification emails like this one, please contact ningji...@apache.org.