Development Best Practices


work in progress


Interacting with the HttpServerExchange object

The helper classes ByteArrayRequest, JsonRequest, ByteArrayResponse and JsonResponse are available to make easy interacting the HttpServerExchange object. As a general rule, always prefer using the helper classes if the functionality you need is available.

For instance the following code snipped retrieves the request JSON content from the HttpServerExchange

HttpServerExchange exchange = ...;

Request request = Request.wrap(exchange);

if (request.isContentTypeJson()) {
  JsonElement content = JsonRequest.wrap(exchange).readContent();

How to send the response

You just set the status code and the response content using helper classes ByteArrayResponse or JsonResponse. You don’t need to send the response explicitly using low level HttpServerExchange methods, since the ResponseSenderHandler is in the processing chain and will do it for you.

public void handleRequest(HttpServerExchange exchange) throws Exception {

  JsonResponse response = JsonResponse.wrap(exchange);

  JsonObject resp = new JsonObject();
  resp.appProperty("message", "OK")