HTTP Examples

The http folder contains examples for the HTTP protocol.

Hello World

An obligatory "Hello World" example for HTTP.

Asynchronous + Aggregated

This example demonstrates asynchronous request processing where the payload body is aggregated into a single object instead of a stream.

  • HelloWorldServer - a server that demonstrates the asynchronous API and responds with a simple Hello World! response body as a text/plain.

  • HelloWorldClient - a client that sends a GET request to the server and receives the response as a single content.

  • HelloWorldUrlClient - a client that sends a GET request to the specified URL in absolute-form and receives the response as a single content.

Asynchronous + Streaming

This example demonstrates asynchronous request processing where the payload body is a stream.

Blocking + Aggregated

This example demonstrates blocking request processing where the payload body is aggregated into a single object. The APIs will block if content is requested but there is no content available.

  • BlockingHelloWorldServer - a server that demonstrates the blocking API and responds with a simple Hello World! response body as a text/plain.

  • BlockingHelloWorldClient - a client that sends a GET request to the server and receives the response payload body as one aggregated object.

  • BlockingHelloWorldUrlClient - a client that sends a GET request to the specified URL in absolute-form and receives the response payload body as one aggregated object.

Blocking + Streaming

This example demonstrates blocking request processing where the payload body is a blocking iterable stream.

Compression

Extends the async "Hello World" example to demonstrate content encoding compression filters. No separate example is needed for the other API variants as the usage of content encoding filters is the same for all API styles.

  • CompressionFilterExampleServer - a server that demonstrates the asynchronous API and responds with a simple Hello World! response body, optionally customized for a specific name for POST requests, as a text/plain.

  • CompressionFilterExampleClient.java - a client that sends a POST request containing a name to the server and receives a response greeting the posted name as a single content.

Debugging

Extends the async "Hello World" example to demonstrate some useful features available for debugging ServiceTalk applications. You should read and understand the async "Hello World" example first to understand the additions this example adds. No separate example is needed for the other API variants as the usage of the debugging features are the same for all API styles.

Timeout

Extends the async "Hello World" example to demonstrate the use of timeout filters and operators. You should read and understand the async "Hello World" example first to understand the additions this example adds. No separate example is needed for the other API variants as the usage of the timeout features are the same for all API styles.

  • TimeoutServer - the async Hello World! server client enhanced to use timeout capabilities.

  • TimeoutClient.java - the async Hello World! client enhanced to use timeout capabilities.

Serialization

An example similar to "Hello World" examples, which demonstrates asynchronous-aggregated, asynchronous-streaming, blocking-aggregated, and blocking-streaming client and server with JSON serialization of simple pojo classes.

Client sends a POST request with a JSON payload PojoRequest and expects a response with Content-Type: application/json and MyPojo as a payload.

JAX-RS

ServiceTalk provides a JAX-RS implementation that can plugin to ServiceTalk APIs. This example demonstrates how to use these APIs, and how different API variations (e.g. asynchronous/blocking and aggregated/streaming) are exposed.

Hello world

A simple "Hello World" example built using JAX-RS.

  • HelloWorldJaxRsServer - a JAX-RS based hello world server that demonstrates how to write blocking as well as asynchronous resource methods.

  • HelloWorldJaxRsResource - a JAX-RS resource having different methods for blocking and asynchronous interactions.

This example does not have a client yet but one can use curl to send requests like:

curl http://localhost:8080/greetings/hello

More examples of how to use the resource can be found in the HelloWorldJaxRsResource javadocs.

MetaData

This example demonstrates some basic functionality of the HttpMetaData classes:

  • Setting and getting response status.

  • Setting and getting query parameters.

  • Setting, checking, and getting headers.

  • Printing headers without redaction/filtering.

Using the following classes:

This example uses the blocking + aggregated API, as the metadata API is the same across all the HTTP APIs.

Mutual TLS

This example demonstrates how client and server can be configured to do mutual authentication via TLS.

Using the following classes:

This example uses the blocking + aggregated API, as the TLS/SSL configuration API is the same across all the HTTP APIs.

HTTP/2

These examples demonstrate how users can configure HTTP/2 transport in ServiceTalk.

HTTP/2 with Prior-Knowledge

This example demonstrates how to configure using HTTP/2 transport with Prior-Knowledge for HTTP clients and servers:

HTTP/2 via ALPN for secure connections

For secure TLS connections ALPN extension could be used to negotiate the communication protocol:

  • HttpServerWithAlpn - A server that negotiates HTTP/2 or HTTP/1.1 using ALPN extension for TLS connections.

  • HttpClientWithAlpn - A client that negotiates HTTP/2 or HTTP/1.1 using ALPN extension for TLS connections.

If HTTP/1.x protocol is configured ServiceTalk always fallbacks to it if the peer does not support ALPN extension.

Your runtime must support ALPN extension for TLS. The recommended way is to use OpenSSL provider and add netty-tcnative artifact to the classpath. If OpenSSL is not available, make sure your JVM version supports ALPN or use another provider that supports it.
These examples use the blocking + aggregated API for demonstration purposes, as the builder API is the same across all the HTTP APIs.

Service Composition

An advanced example which demonstrates a composition of various ServiceTalks services in one application. For more information see Service Composition.

Unix Domain Sockets (UDS)

This example demonstrates how client and server can use unix domain sockets. See the uds example code for more details.

This example uses the blocking + aggregated API, as the UDS configuration API is the same across all the HTTP APIs.