OData
Since Camel 4.23
Only producer is supported
The OData component allows you to interact with OData V4 services using standard OData CRUD operations.
The component provides OData-specific request and response handling while using Camel HTTP as the underlying HTTP transport.
The OData component is the successor to the deprecated camel-olingo4 component.
The component supports OData V4 services using JSON representations.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-odata</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> URI Format
odata:httpUri
Where httpUri is the base URL of the OData entity set or resource.
For example:
odata:http://localhost:8080/odata/Products
odata:https://services.odata.org/V4/Northwind/Northwind.svc/Categories The URI can also contain endpoint options as query parameters.
The OData query options can alternatively be configured on the endpoint or supplied dynamically using message headers.
The component uses Camel HTTP for the underlying HTTP transport.
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The OData component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean |
Endpoint Options
The OData endpoint is configured using URI syntax:
odata:httpUri
With the following path and query parameters:
Path Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Required The URL of the OData entity set or resource, such as http://localhost:8080/odata/Products. For READ_ENTRY, UPDATE and DELETE the key of the CamelODataKey header is appended to it in parentheses. Its scheme (http or https) selects the Camel HTTP component that sends the requests. | URI |
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Whether to send the $count query option, which asks the service to return the total number of matching entities (true) or not (false). The component sets the returned count in the CamelODataCount header. When not set, $count is not sent. The CamelODataIncludeCount header overrides this option. | Boolean | ||
The $expand query option: a comma-separated list of the navigation properties whose related entities to include in the response, such as Category. The CamelODataExpand header overrides this option. | String | ||
The $filter query option: an expression that selects the entities to return, such as Name eq 'Laptop'. The CamelODataFilter header overrides this option. | String | ||
The OData operation to perform: READ_SET reads a collection of entities (GET), READ_ENTRY reads one entity (GET), CREATE creates an entity from the message body (POST), UPDATE changes the properties of an entity that are in the message body (PATCH) and DELETE deletes an entity (DELETE). READ_ENTRY, UPDATE and DELETE take the key of the entity from the CamelODataKey header. The CamelODataOperation header overrides this option. Enum values:
| READ_SET | ODataOperation | |
The $orderby query option: a comma-separated list of the properties to sort by, each optionally followed by asc or desc, such as Name desc. The CamelODataOrderBy header overrides this option. | String | ||
The $select query option: a comma-separated list of the properties to return, such as ID,Name. The CamelODataSelect header overrides this option. | String | ||
The $skip query option: the number of entities to skip before the first one returned. The CamelODataSkip header overrides this option. | Integer | ||
The $top query option: the maximum number of entities to return. The CamelODataTop header overrides this option. | Integer | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Bearer token for Bearer authentication. | String | ||
Authentication method to use (e.g., Basic, Bearer). | String | ||
Password for Basic authentication. | String | ||
Username for Basic authentication. | String | ||
The SSL/TLS configuration (SSLContextParameters) of the connections to the OData service, passed to the Camel HTTP endpoint that sends the requests. | SSLContextParameters | ||
Whether to use the global SSL context parameters of the CamelContext when sslContextParameters is not set. | false | boolean |
Message Headers
The OData component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelODataOperation (producer) Constant: | The OData operation to perform, overriding the operation option. Enum values:
| ODataOperation | |
| Constant: | The key of the entity for READ_ENTRY, UPDATE and DELETE, appended to the endpoint URI in parentheses, such as 101 or 'ABC-123'. The value is used as it is, so a string key needs its quotes. | String | |
| Constant: | The ETag of the entity. When set, it is sent in the If-Match request header, so that the service applies the request only if the entity still has this ETag. After the call, the ETag of the response, from its ETag header or its odata.etag control information. | String | |
| Constant: | The URL of the next page of results, from the odata.nextLink control information of the response. | String | |
CamelODataIncludeCount (producer) Constant: | Whether to send the $count query option, overriding the count option. | Boolean | |
| Constant: | The total number of matching entities, from the odata.count control information of the response. | Long | |
| Constant: | The $filter query option, overriding the filter option. | String | |
| Constant: | The $select query option, overriding the select option. | String | |
| Constant: | The $expand query option, overriding the expand option. | String | |
| Constant: | The $orderby query option, overriding the orderBy option. | String | |
| Constant: | The $top query option, overriding the top option. | Integer | |
| Constant: | The $skip query option, overriding the skip option. | Integer | |
| Constant: | The Authorization header of the HTTP request to the OData service. When it is set, it is sent as it is and takes precedence over the authentication configured on the endpoint (authMethod, authBearerToken). | String | |
CamelHttpResponseCode (producer) Constant: | The HTTP status code of the response of the OData service. | Integer |
Message Body
The message body at each side of the OData component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| The body is not read by READ_SET, READ_ENTRY and DELETE. CREATE and UPDATE send JSON text as a String, bytes, a stream or a file as the JSON payload as it is, and serialize a Map or a List to JSON; a POJO fails, and so does an empty body (use {} for an empty JSON object). The operation can also be set with the CamelODataOperation header.
|
Producer output |
| The JSON object of the response parsed into a Map (a JsonObject); for READ_SET the entities are in its value entry. A JSON array is parsed into a List (a JsonArray), and a plain value into a number, a String or a boolean. No body for a 204 or an empty response, such as of a DELETE. The response code, the ETag, the count and the next link are in headers. |
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
| This is a producer-only component. Per-message options can be supplied through |
| If the route receives messages from an untrusted producer, take care when forwarding internal Camel headers to this component. In particular, an inbound |
Operations
The operation is selected using the CamelODataOperation message header or the operation endpoint option.
The following operations are supported:
| Operation | HTTP Method | Description |
|---|---|---|
|
| Reads a collection of entities. This is the default operation. |
|
| Reads a single entity. Use |
|
| Creates a new entity using the message body as the request payload. |
|
| Updates an existing entity using |
|
| Deletes an existing entity using |
For READ_ENTRY, UPDATE, and DELETE, the key is appended to the configured resource URI.
For example:
CamelODataKey = 101
odata:http://localhost:8080/odata/Products results in:
http://localhost:8080/odata/Products(101) The key is treated as an OData key expression. The component does not automatically add quotes or otherwise change the supplied key value.
For example, a string key can be supplied as 'ABC-123', while a composite key can be supplied as ProductID=101,CategoryID=5.
The key value should be a valid and trusted OData key expression.
Message Body
CREATE and UPDATE send the JSON of the entity from the message body:
-
JSON text as a
String, a byte array, a stream or a file is sent as it is. -
A
Mapor aListis serialized to JSON.
Other Java objects (POJOs) are not serialized: marshal them to JSON first, for example with the JSON data format. An empty body fails, as these operations need an entity: set the body to {} to send an empty JSON object.
The other operations do not read the message body.
Message Headers
The following message headers can be used to control OData processing dynamically.
| Header | Type | Description |
|---|---|---|
|
| Specifies the operation to execute. |
|
| Specifies the key for |
|
| Overrides the |
|
| Overrides the |
|
| Overrides the |
|
| Overrides the |
|
| Overrides the |
|
| Overrides the |
|
| Controls whether the |
|
| Supplies an ETag for the |
|
| Contains the count returned by the OData service when an |
|
| Contains the |
|
| Contains the HTTP response status code returned by the OData service. |
A message-level Authorization header can also be supplied.
When an Authorization header is present, it takes precedence over authentication configured on the OData endpoint.
Authentication configured on the endpoint is applied only to the delegated HTTP request and does not add an Authorization header to the result. A caller-supplied Authorization header remains on the exchange.
The CamelHttpUri, CamelHttpQuery, CamelHttpMethod and Accept headers that the OData producer sets for the delegated HTTP request are also excluded from the result. Caller-supplied values for these headers remain on the exchange.
If the OData route is exposed to an upstream HTTP consumer, such as platform-http, take care that an inbound Authorization header may be forwarded to the OData service.
If the upstream authorization credentials must not be forwarded, remove the header before sending the exchange to the OData component.
For example:
-
Java
-
XML
-
YAML
from("platform-http:/products")
.removeHeader("Authorization")
.to("odata:http://my-odata-service/odata/Products"); <route>
<from uri="platform-http:/products"/>
<removeHeader name="Authorization"/>
<to uri="odata:http://my-odata-service/odata/Products"/>
</route> - route:
from:
uri: platform-http:/products
steps:
- removeHeader:
name: Authorization
- to:
uri: odata:http://my-odata-service/odata/Products Query Options
The component supports the following OData query options:
| Option | Type | Description |
|---|---|---|
|
| OData |
|
| OData |
|
| OData |
|
| OData |
|
| OData |
|
| OData |
|
| Controls the OData |
Additional OData query options can be supplied as endpoint URI parameters. Because the component uses lenient endpoint properties, options that are not defined as explicit component options are passed through as OData query parameters. Ensure that endpoint option names are spelled correctly and that any additional options are valid OData query options.
For example:
odata:http://localhost:8080/odata/Products?filter=Name%20eq%20%27Laptop%27&select=Name,Price&top=10 Query options configured through message headers take precedence over the corresponding endpoint configuration.
Authentication
The component supports Basic authentication and Bearer token authentication through endpoint options.
For example, Basic authentication can be configured using:
odata:http://localhost:8080/odata/Products?authMethod=Basic&authUsername=user&authPassword=password Bearer token authentication can be configured using the authBearerToken option.
Authentication can also be supplied dynamically using the message-level Authorization header:
-
Java
-
XML
-
YAML
from("direct:getProducts")
.setHeader("Authorization", constant("Bearer my-token"))
.to("odata:http://my-odata-service/odata/Products"); <route>
<from uri="direct:getProducts"/>
<setHeader name="Authorization">
<constant>Bearer my-token</constant>
</setHeader>
<to uri="odata:http://my-odata-service/odata/Products"/>
</route> - route:
from:
uri: direct:getProducts
steps:
- setHeader:
name: Authorization
expression:
constant:
expression: "Bearer my-token"
- to:
uri: odata:http://my-odata-service/odata/Products When an Authorization header is present on the message, it takes precedence over endpoint authentication.
Take care when forwarding exchanges from an upstream HTTP consumer because the incoming Authorization header may be forwarded to the OData service.
ETags
The component supports optimistic concurrency using OData/HTTP ETags.
An ETag supplied in CamelODataETag is sent to the service using the HTTP If-Match header.
For example:
-
Java
-
XML
-
YAML
from("direct:updateProduct")
.setHeader("CamelODataOperation", constant("UPDATE"))
.setHeader("CamelODataKey", constant("101"))
.setHeader("CamelODataETag", constant("W/\"etag-value\""))
.setBody(constant("{\"Price\": 249.99}"))
.to("odata:http://my-odata-service/odata/Products"); <route>
<from uri="direct:updateProduct"/>
<setHeader name="CamelODataOperation">
<constant>UPDATE</constant>
</setHeader>
<setHeader name="CamelODataKey">
<constant>101</constant>
</setHeader>
<setHeader name="CamelODataETag">
<constant>W/"etag-value"</constant>
</setHeader>
<setBody>
<constant>{"Price": 249.99}</constant>
</setBody>
<to uri="odata:http://my-odata-service/odata/Products"/>
</route> - route:
from:
uri: direct:updateProduct
steps:
- setHeader:
name: CamelODataOperation
expression:
constant:
expression: UPDATE
- setHeader:
name: CamelODataKey
expression:
constant:
expression: "101"
- setHeader:
name: CamelODataETag
expression:
constant:
expression: 'W/"etag-value"'
- setBody:
expression:
constant:
expression: '{"Price": 249.99}'
- to:
uri: odata:http://my-odata-service/odata/Products When the service returns an HTTP ETag header, the value is exposed as CamelODataETag.
The component also recognizes the OData @odata.etag property in a JSON response.
OData Response Metadata
For responses containing OData metadata, the component exposes commonly used values as Camel message headers.
For example, a response containing:
{
"@odata.count": 42,
"@odata.nextLink": "https://my-odata-service/odata/Products?$skip=10",
"value": [
{
"ID": 1,
"Name": "Laptop"
}
]
} makes the count and next-link available through:
exchange.getMessage().getHeader("CamelODataCount");
exchange.getMessage().getHeader("CamelODataNextLink"); The response body contains the parsed JSON response: a JSON object is a Map, a JSON array a List, and a plain value a number, a String or a boolean. A 204 or an empty response gives no body.
SSL Configuration
The component uses Camel HTTP for the underlying HTTP transport.
The sslContextParameters endpoint option can be used to configure SSL/TLS parameters for HTTPS connections.
Refer to the generated endpoint options section for details about configuring SSL context parameters.
Examples
Fetching a Collection
-
Java
-
XML
-
YAML
from("direct:getProducts")
.to("odata:http://my-odata-service/odata/Products")
.split(simple("${body[value]}"))
.to("log:product")
.end(); <route>
<from uri="direct:getProducts"/>
<to uri="odata:http://my-odata-service/odata/Products"/>
<split>
<simple>${body[value]}</simple>
<to uri="log:product"/>
</split>
</route> - route:
from:
uri: direct:getProducts
steps:
- to:
uri: odata:http://my-odata-service/odata/Products
- split:
expression:
simple:
expression: "${body[value]}"
steps:
- to:
uri: log:product The default operation is READ_SET, so no operation header is required. The response body is the parsed JSON object as a Map, and the entities are in its value entry.
Reading a Single Entity
-
Java
-
XML
-
YAML
from("direct:getProductById")
.setHeader("CamelODataOperation", constant("READ_ENTRY"))
.setHeader("CamelODataKey", constant("101"))
.to("odata:http://my-odata-service/odata/Products"); <route>
<from uri="direct:getProductById"/>
<setHeader name="CamelODataOperation">
<constant>READ_ENTRY</constant>
</setHeader>
<setHeader name="CamelODataKey">
<constant>101</constant>
</setHeader>
<to uri="odata:http://my-odata-service/odata/Products"/>
</route> - route:
from:
uri: direct:getProductById
steps:
- setHeader:
name: CamelODataOperation
expression:
constant:
expression: READ_ENTRY
- setHeader:
name: CamelODataKey
expression:
constant:
expression: "101"
- to:
uri: odata:http://my-odata-service/odata/Products This results in a request to:
http://my-odata-service/odata/Products(101) Creating an Entity
-
Java
-
XML
-
YAML
from("direct:createProduct")
.setHeader("CamelODataOperation", constant("CREATE"))
.setBody(constant("{\"Name\": \"Tablet\", \"Price\": 299.99}"))
.to("odata:http://my-odata-service/odata/Products"); <route>
<from uri="direct:createProduct"/>
<setHeader name="CamelODataOperation">
<constant>CREATE</constant>
</setHeader>
<setBody>
<constant>{"Name": "Tablet", "Price": 299.99}</constant>
</setBody>
<to uri="odata:http://my-odata-service/odata/Products"/>
</route> - route:
from:
uri: direct:createProduct
steps:
- setHeader:
name: CamelODataOperation
expression:
constant:
expression: CREATE
- setBody:
expression:
constant:
expression: '{"Name": "Tablet", "Price": 299.99}'
- to:
uri: odata:http://my-odata-service/odata/Products The component sends the message body using HTTP POST. A String body is sent as is, so it must be JSON; a Map or a List is serialized to JSON, as described in Message Body.
Updating an Entity
-
Java
-
XML
-
YAML
from("direct:updateProduct")
.setHeader("CamelODataOperation", constant("UPDATE"))
.setHeader("CamelODataKey", constant("101"))
.setHeader("CamelODataETag", constant("W/\"etag-value\""))
.setBody(constant("{\"Price\": 249.99}"))
.to("odata:http://my-odata-service/odata/Products"); <route>
<from uri="direct:updateProduct"/>
<setHeader name="CamelODataOperation">
<constant>UPDATE</constant>
</setHeader>
<setHeader name="CamelODataKey">
<constant>101</constant>
</setHeader>
<setHeader name="CamelODataETag">
<constant>W/"etag-value"</constant>
</setHeader>
<setBody>
<constant>{"Price": 249.99}</constant>
</setBody>
<to uri="odata:http://my-odata-service/odata/Products"/>
</route> - route:
from:
uri: direct:updateProduct
steps:
- setHeader:
name: CamelODataOperation
expression:
constant:
expression: UPDATE
- setHeader:
name: CamelODataKey
expression:
constant:
expression: "101"
- setHeader:
name: CamelODataETag
expression:
constant:
expression: 'W/"etag-value"'
- setBody:
expression:
constant:
expression: '{"Price": 249.99}'
- to:
uri: odata:http://my-odata-service/odata/Products The component sends the update using HTTP PATCH.
When an ETag is supplied, it is sent using the HTTP If-Match header.
Deleting an Entity
-
Java
-
XML
-
YAML
from("direct:deleteProduct")
.setHeader("CamelODataOperation", constant("DELETE"))
.setHeader("CamelODataKey", constant("101"))
.to("odata:http://my-odata-service/odata/Products"); <route>
<from uri="direct:deleteProduct"/>
<setHeader name="CamelODataOperation">
<constant>DELETE</constant>
</setHeader>
<setHeader name="CamelODataKey">
<constant>101</constant>
</setHeader>
<to uri="odata:http://my-odata-service/odata/Products"/>
</route> - route:
from:
uri: direct:deleteProduct
steps:
- setHeader:
name: CamelODataOperation
expression:
constant:
expression: DELETE
- setHeader:
name: CamelODataKey
expression:
constant:
expression: "101"
- to:
uri: odata:http://my-odata-service/odata/Products The component sends an HTTP DELETE request for the specified entity.