Skip to content

Latest commit

 

History

History
178 lines (103 loc) · 6.86 KB

README.adoc

File metadata and controls

178 lines (103 loc) · 6.86 KB

Vert.x Web-Client examples

Here you will find examples demonstrating Vert.x Web Client in action.

Vert.x Web Client is an asynchronous HTTP and HTTP/2 client. Please consult the Vert.x Web Client manual for detailed documentation on Vert.x Web Client.

Logging

When running in an IDE you can edit src/main/resources/vertx-default-jul-logging.properties to configure logging. Trace logging is enabled for Vert.x Web Client classes, so you can easily trace requests as they are routed through different handlers.

Dependencies required

To use Vert.x Web Client in your own Maven or Gradle project you will need following dependencies

Group ID: io.vertx
Artifact ID: vertx-core

and

Group ID: io.vertx
Artifact ID: vertx-web-client

If you’re using a template engine you will also need to add the engine dependency explicitly, depending on the engine you are using.

Simple

This example uses the web client to send a request and log the response.

First you need to run the server then you can run the client.

Query params

This example uses the web client to send a request with query params using the fluent API and log the response.

First you need to run the server then you can run the client.

The server logs the query string on the console.

The client add several query parameters to the http request with the fluent API, the client takes care of encoding the parameters.

URI Templates

This example uses the web client to send a request with query params using the fluent API and log the response.

This example is pretty much similar to the query params example, however it leverages Vert.x URI Templates to encode the query parameters in the request URI.

First you need to run the server then you can run the client.

The server logs the query string on the console.

HTTPS

This example is like the simple example but shows how to configure the client for sending SSL/TLS requests.

First you need to run the server then you can run the client.

Sending bodies

The web client is able to send various kinds of bodies to the server.

Hello World

This examples shows how to send a body with "Hello World" with the web client.

First you need to run the server then you can run the client.

Sending a form

This examples shows how to send a form with the web client.

First you need to run the server then you can run the client.

Sending a json object

This examples shows how to send a json object with the web client.

First you need to run the server then you can run the client.

Sending a POJO mapped to json

This examples shows how to send a simple java object encoded as a json object with the web client.

First you need to run the server then you can run the client.

Sending a multipart form

This examples shows how to send a multipart form with the web client.

First you need to run the server then you can run the client.

Sending a stream

This examples shows how to send a vertx read stream with the web client.

The web client takes care of setting up the pipe between the read stream and the http client request.

First you need to run the server then you can run the client.

Receiving response bodies

The web client can unmarshall response bodies to various formats.

Receiving a json object body

This examples shows how to decode a json object body with the web client.

First you need to run the server then you can run the client.

Receiving a POJO mapped to json

This examples shows how to decode a json object body to a simple java object with the web client.

First you need to run the server then you can run the client.

Receiving a OAUTH2 protected resource

This examples shows how to download an oauth2 protected resource, the example makes use of 2 requests:

  • first acquire a OAUTH2 access token

  • get the resource using the token

Receiving a OAUTH2 protected resource using OAuth2WebClient

This example shows how to utilise the OAuth2WebClient which simplifies the process of acquiring and managing an OAuth2 access token and using it to access a protected resource.

First you configure the OAuth2 flow and then you create the client using the specified configuration. When sending request to the protected resource, the client will automatically acquire the token if it is not already available or is expired. Flow that will be used to acquire the token is specified per request.