Vert.x HTTP Client
Since Camel 3.5
Only producer is supported
The Vert.x HTTP component provides the capability to produce messages to HTTP endpoints via the Vert.x Web Client.
Maven users will need to add the following dependency to their pom.xml
for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-vertx-http</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
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
,*.yaml
files, 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 Vert.x HTTP Client component supports 19 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 the response body should be byte or as io.vertx.core.buffer.Buffer. | true | boolean | |
Whether to allow java serialization when a request has the Content-Type application/x-java-serialized-object This is disabled by default. If you enable this, be aware that Java will deserialize the incoming data from the request. This can be a potential security risk. | 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 | |
To use an existing vertx instead of creating a new instance. | Vertx | ||
A custom VertxHttpBinding which can control how to bind between Vert.x and Camel. | VertxHttpBinding | ||
To provide a custom set of vertx options for configuring vertx. | VertxOptions | ||
To provide a custom set of options for configuring vertx web client. | WebClientOptions | ||
To use a custom org.apache.camel.spi.HeaderFilterStrategy to filter header to and from Camel message. | HeaderFilterStrategy | ||
The proxy server host address. | String | ||
The proxy server password if authentication is required. | String | ||
The proxy server port. | Integer | ||
The proxy server type. Enum values:
| ProxyType | ||
The proxy server username if authentication is required. | String | ||
The password to use for basic authentication. | String | ||
The user name to use for basic authentication. | String | ||
The bearer token to use for bearer token authentication. | String | ||
To configure security using SSLContextParameters. | SSLContextParameters | ||
Enable usage of global SSL context parameters. | false | boolean |
Endpoint Options
The Vert.x HTTP Client endpoint is configured using URI syntax:
vertx-http:httpUri
With the following path and query parameters:
Query Parameters (23 parameters)
Name | Description | Default | Type |
---|---|---|---|
The amount of time in milliseconds until a connection is established. A timeout value of zero is interpreted as an infinite timeout. | 60000 | int | |
A custom CookieStore to use when session management is enabled. If this option is not set then an in-memory CookieStore is used. | InMemoryCookieStore | CookieStore | |
A custom org.apache.camel.spi.HeaderFilterStrategy to filter header to and from Camel message. | VertxHttpHeaderFilterStrategy | HeaderFilterStrategy | |
The HTTP method to use. The HttpMethod header cannot override this option if set. Enum values:
| HttpMethod | ||
The status codes which are considered a success response. The values are inclusive. Multiple ranges can be defined, separated by comma, e.g. 200-204,209,301-304. Each range must be a single number or from-to with the dash included. | 200-299 | String | |
Whether the response body should be byte or as io.vertx.core.buffer.Buffer. | true | boolean | |
Enables session management via WebClientSession. By default the client is configured to use an in-memory CookieStore. The cookieStore option can be used to override this. | false | boolean | |
Disable throwing HttpOperationFailedException in case of failed responses from the remote server. | true | boolean | |
The amount of time in milliseconds after which if the request does not return any data within the timeout period a TimeoutException fails the request. Setting zero or a negative value disables the timeout. | -1 | long | |
If enabled and an Exchange failed processing on the consumer side, and if the caused Exception was sent back serialized in the response as a application/x-java-serialized-object content type. On the producer side the exception will be deserialized and thrown as is, instead of HttpOperationFailedException. The caused exception is required to be serialized. This is by default turned off. If you enable this then be aware that Camel will deserialize the incoming data from the request to a Java object, which can be a potential security risk. | false | boolean | |
Set whether compression is enabled to handled compressed (E.g gzipped) responses. | false | boolean | |
A custom VertxHttpBinding which can control how to bind between Vert.x and Camel. | VertxHttpBinding | ||
Sets customized options for configuring the Vert.x WebClient. | WebClientOptions | ||
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 | |
The proxy server host address. | String | ||
The proxy server password if authentication is required. | String | ||
The proxy server port. | Integer | ||
The proxy server type. Enum values:
| ProxyType | ||
The proxy server username if authentication is required. | String | ||
The password to use for basic authentication. | String | ||
The user name to use for basic authentication. | String | ||
The bearer token to use for bearer token authentication. | String | ||
To configure security using SSLContextParameters. | SSLContextParameters |
Message Headers
The Vert.x HTTP Client component supports 8 message header(s), which is/are listed below:
Name | Description | Default | Type |
---|---|---|---|
Constant: | The http method. | HttpMethod | |
CamelHttpResponseCode (producer) Constant: | The HTTP response code from the external server. | Integer | |
CamelHttpResponseText (producer) Constant: | The HTTP response text from the external server. | String | |
Constant: | The HTTP content type. Is set on both the IN and OUT message to provide a content type, such as text/html. | String | |
Constant: | URI parameters. Will override existing URI parameters set directly on the endpoint. | String | |
Constant: | URI to call. Will override the existing URI set directly on the endpoint. This URI is the URI of the http server to call. Its not the same as the Camel endpoint URI, where you can configure endpoint options such as security etc. This header does not support that, its only the URI of the http server. | String | |
Constant: | Request URI’s path, the header will be used to build the request URI with the HTTP_URI. | String | |
Constant: | The HTTP content encoding. Is set to provide a content encoding, such as gzip. | String |
Usage
The following example shows how to send a request to an HTTP endpoint.
You can override the URI configured on the vertx-http
producer via headers Exchange.HTTP_URI
and Exchange.HTTP_PATH
.
from("direct:start")
.to("vertx-http:https://camel.apache.org");
URI Parameters
The vertx-http
producer supports URI parameters to be sent to the HTTP server. The URI parameters can either be set directly on the endpoint URI, or as a header with the key Exchange.HTTP_QUERY
on the message.
Response code
Camel will handle, according to the HTTP response code:
-
Response code is in the range 100..299, Camel regards it as a success response.
-
Response code is in the range 300..399, Camel regards it as a redirection response and will throw a
HttpOperationFailedException
with the information. -
Response code is 400+, Camel regards it as an external server failure and will throw a
HttpOperationFailedException
with the information.
throwExceptionOnFailure
The option, throwExceptionOnFailure
, can be set to false
to prevent the HttpOperationFailedException
from being thrown for failed response codes. This allows you to get any response from the remote server.
Exceptions
HttpOperationFailedException
exception contains the following information:
-
The HTTP status code
-
The HTTP status line (text of the status code)
-
Redirect location if server returned a redirect
-
Response body as a
java.lang.String
, if server provided a body as response
HTTP method
The following algorithm determines the HTTP method to be used:
1. Use method provided as endpoint configuration (httpMethod
).
2. Use method provided in header (Exchange.HTTP_METHOD
).
3. GET
if query string is provided in header.
4. GET
if endpoint is configured with a query string.
5. POST
if there is data to send (body is not null
).
6. GET
otherwise.
HTTP form parameters
You can send HTTP form parameters in one of two ways.
-
Set the
Exchange.CONTENT_TYPE
header to the valueapplication/x-www-form-urlencoded
and ensure the message body is aString
formatted as form variables. For exampleparam1=value1¶m2=value2
. -
Set the message body as a MultiMap which allows you to configure form parameter names and values.
Multipart form data
You can upload text or binary files by setting the message body as a MultipartForm.
Customizing Vert.x Web Client options
When finer control of the Vert.x Web Client configuration is required, you can bind a custom WebClientOptions instance to the registry.
WebClientOptions options = new WebClientOptions().setMaxRedirects(5)
.setIdleTimeout(10)
.setConnectTimeout(3);
camelContext.getRegistry.bind("clientOptions", options);
Then reference the options on the vertx-http
producer.
from("direct:start")
.to("vertx-http:http://localhost:8080?webClientOptions=#clientOptions")
SSL
The Vert.x HTTP component supports SSL/TLS configuration through the Camel JSSE Configuration Utility.
It is also possible to configure SSL options by providing a custom WebClientOptions
.
Session Management
Session management can be enabled via the sessionManagement
URI option. When enabled, an in-memory cookie store is used to track cookies. This can be overridden by providing a custom CookieStore
via the cookieStore
URI option.
Spring Boot Auto-Configuration
When using vertx-http with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-vertx-http-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
The component supports 20 options, which are listed below.