...
When using CXF as a consumer, the CXF Bean Component allows you to factor out how message payloads are received from their processing as a RESTful or SOAP web service. This has the potential of using a multitude of transports to consume web services. The bean component's configuration is also simpler and provides the fastest method to implement web services using Camel and CXF.
The cxfrs: component provides integration with Apache CXF for connecting to JAX-RS 1.1 and 2.0 services hosted in CXF.
Maven users will need to add the following dependency to their pom.xml for this component:
...
...
URI format
...
Where address represents the CXF endpoint's address
...
...
Where rsEndpoint represents the spring bean's name which presents the CXFRS client or server
For either style above, you can append options to the URI as follows:
...
...
Options
...
Name | Description | Example | Required? | default value |
---|---|---|---|---|
| The resource classes which you want to export as REST service. Multiple classes can be separated by comma. |
|
...
|
...
No | None | |
| Deprecated: Use |
|
...
No | None | |||
| new to Camel 2.1 If it is true, the CxfRsProducer will use the HttpClientAPI to invoke the service | httpClientAPI=true | No | true |
| This option will let you decide to use sync or async API to do the underlying work. The default value is false which means it will try to use async API by default. This option is available as of 2.5 for CxfRsConsumer and as of 2.19 for CxfRsProducer. | synchronous=true | No | false |
| New in 2.6, this option tells the CxfRsProducer to inspect return codes and will generate an Exception if the return code is larger than 207. | throwExceptionOnFailure=true | No | true |
| New in 2.6, you can set a IN message header CamelDestinationOverrideUrl to dynamically override the target destination Web Service or REST Service defined in your routes. The implementation caches CXF clients or ClientFactoryBean in CxfProvider and CxfRsProvider. This option allows you to configure the maximum size of the cache. | maxClientCacheSize=5 | No | 10 |
| New in 2.9.0. deprecated use defaultBus option from Camel 2.16 onwards. Will set the default bus when CXF endpoint create a bus by itself |
| No | false |
defaultBus | Camel 2.16: Will set the default bus when CXF endpoint create a bus by itself | defaultBus=true | No | false |
| New in 2.9.0. A default bus created by CXF Bus Factory. Use |
| No | None |
| As of 2.11. Sets how requests and responses will be mapped to/from Camel. Two values are possible:
|
| No | Default |
| Allows you to specify a custom CxfRsBinding implementation to perform low-level processing of the raw CXF request and response objects. The implementation must be bound in the Camel registry, and you must use the hash (#) notation to refer to it. | binding=#myBinding | No | DefaultCxfRsBinding |
| Since Camel 2.12.2 set custom JAX-RS providers list to the CxfRs endpoint. |
| No | None |
| Since Camel 2.12.2 Sets the locations of the schemas which can be used to validate the incoming XML or JAXB-driven JSON. |
| No | None |
| Since Camel 2.12.3 Set the feature list to the CxfRs endpoint. |
| No | None |
| Since Camel 2.12.4 Set the properties to the CxfRs endpoint. |
| No | None |
| Since Camel 2.12.4 Set the inInterceptors to the CxfRs endpoint. |
| No | None |
| Since Camel 2.12.4 Set the outInterceptor to the CxfRs endpoint. |
| No | None |
| Since Camel 2.12.4 Set the inFaultInterceptors to the CxfRs endpoint. |
| No | None |
| Since Camel 2.12.4 Set the outFaultInterceptors to the CxfRs endpoint. |
| No | None |
| Since Camel 2.14.0 This option is used to set the CXF continuation timeout which could be used in CxfConsumer by default when the CXF server is using Jetty or Servlet transport. (Before Camel 2.14.0, CxfConsumer just set the continuation timeout to be 0, which means the continuation suspend operation never timeout.) |
| No | 30000 |
ignoreDeleteMethodMessageBody | Since Camel 2.14.1 This option is used to tell CxfRsProducer to ignore the message body of the DELETE method when using HTTP API. | ignoreDeleteMethodMessageBody=true | No | false |
| Since Camel 2.14.2 This option is used to specify the model file which is useful for the resource class without annotation. Since Camel 2.15 This option can point to a model file without specifying a service class for emulating document-only endpoints |
| No | None |
| Since Camel 2.15 When the option is true, camel will perform the invocation of the resource class instance and put the response object into the exchange for further processing. |
| No | false |
propagateContexts | Since Camel 2.15 When the option is true, JAXRS UriInfo, HttpHeaders, Request and SecurityContext contexts will be available to custom CXFRS processors as typed Camel exchange properties. These contexts can be used to analyze the current requests using JAX-RS API. | |||
loggingFeatureEnabled | This option enables CXF Logging Feature which writes inbound and outbound REST messages to log. | No | false | |
skipFaultLogging | This option controls whether the PhaseInterceptorChain skips logging the Fault that it catches. | No | false | |
loggingSizeLimit | To limit the total size of number of bytes the logger will output when logging feature has been enabled. | No | 0 | |
cookieHandler | Since Camel 2.19.0 Configure a cookie handler to maintain a HTTP session | cookieHandler=#exchangeCookieHandler | No | None |
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 provide different configuration for them.
Please check out the schema file and CXF JAX-RS documentation for more information.
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.
You can find a Camel REST service route configuration example here.
How to override the CXF producer address from message header
The camel-cxfrs
producer supports to override the services address by setting the message with the key of "CamelDestinationOverrideUrl".
...
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 provide different configuration for them.
Please check out the schema file and CXF REST user guide for more information.
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.
You can find a Camel REST service route configuration example here.
...
Consuming a REST Request - Simple Binding Style
...
This binding style can be activated by setting the bindingStyle
parameter in the consumer endpoint to value SimpleConsumer
:
...
...
Examples of request binding with different method signatures
...
Given a JAX-RS resource class with this method:
...
Serviced by the following route:
...
...
The following HTTP request with XML payload (given that the Customer DTO is JAXB-annotated):
...
Will print the message:
...
...
For more examples on how to process requests and write responses can be found here.
Consuming a REST Request - Default Binding Style
The CXF JAXRS front end implements the JAXRS(JSR311 JAX-RS (JSR-311) 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.
Unlike the camel-restlet
Camel Restlet component, you don't need to specify the URI template within your restlet endpoint, CXF take takes care of the REST request URI to resource class method mapping according to the JSR311 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...
...
...
And the corresponding resource class used to configure the endpoint...
Info | ||
---|---|---|
| ||
This class is used to configure the JAXRS properties ONLY. The methods will NOT be executed during the routing of messages to the endpoint, the route itself is responsible for ALL processing instead. |
...
the corresponding resource class used to configure the endpoint...
...
By default, JAX-RS resource classes are only used to configure JAX-RS properties. Methods will not be executed during routing of messages to the endpoint. Instead, it is the responsibility of the route to do all processing.
Note that starting from Camel 2.15 it is also sufficient to provide an interface only as opposed to a no-op service implementation class for the default mode.
Starting from Camel 2.15, if a performInvocation option is enabled, the service implementation will be invoked first, the response will be 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.
How to invoke the REST service through camel-cxfrs producer
The CXF JAXRS front end implements a 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 proxy API.
So, you You just need to specify the operation name in the message header and prepare the parameter in the message body, the camel-cxfrs producer producer will generate right REST request for you.
Here is an example
...
:
...
The CXF JAXRS front end also provides a http centric client API, You . 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 the HTTP_METHOD and let the producer use the http centric client API by using the URI option httpClientAPI or set by setting 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 specified with the message header CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS.
...
...
From Camel 2.1, we also support to specify the query parameters from cxfrs URI for the CXFRS http centric client.
...
...
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.
...