Java single dependency Dockerized HTTP endpoint

Java single dependency Dockerized HTTP endpoint

We will create a Java-based HTTP endpoint, make an executable jar out of it, pack it up in Docker, and run it locally in no time.

This article is intended for beginners, who want to looking for a simple walk-through for running a Java application in Docker.

The vast majority of examples out there describing Java applications in a Dockerized environment include the usage of heavy frameworks like Spring Boot. We want to show here that you don't need much to get an endpoint running with Java in Docker.

In fact, we will only use a single library as a dependency: HttpMate core. For this example, we'll use the LowLevel builder of HttpMate with a single HTTP handler.

The environment used for this example

  • Java 11+
  • Maven 3.5+
  • Java-friendly IDE
  • Docker version 18+
  • Basic understanding of HTTP/bash/Java

The final result is available in this git repo.

Organizing the Project

Let's create our initial project structure:

mkdir -p simple-java-http-docker/src/main/java/com/envimate/examples/http

Let's start with the project's pom file in the root directory that we called here simple-java-http-docker:

<project xmlns:xsi="" xmlns=""




Here we have:

  • The standard groupId/artifactId/version definition for our project.
  • The single dependency on the HttpMate core library.

This is enough to start developing our endpoint in the IDE of choice. Most of those have support for Maven-based Java projects.

Application Entrypoint

To start our little server, we will use a simple main method. Let's create the entry to our application as an file in the directory src/main/java/com/envimate/examples/http that will for now just output the time to the console.

package com.envimate.examples.http;

import java.time.LocalDateTime; import java.time.format.DateTimeFormatter;

public final class Application { public static void main(String[] args) { final LocalDateTime time =; final String dateFormatted = time.format(DateTimeFormatter.ISO_TIME); System.out.println("current time is " + dateFormatted); } }

Try to run this class and you will see the current time printed.

Let's make this more functional and separate the part that prints out the time into a lambda function with no argument, a.k.a Supplier.

package com.envimate.examples.http;

import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.function.Supplier;

public final class Application { public static void main(String[] args) { Supplier handler = () -> { final LocalDateTime time =; final String dateFormatted = time.format(DateTimeFormatter.ISO_TIME); return "current time is " + dateFormatted; };



The convenience interface provided by the low-level HttpMate does not look much different, except instead of returning a String, that String is set to the response, alongside with the indication that everything went well (a.k.a. response code 200).

final HttpHandler httpHandler = (request, response) -> { final LocalDateTime time =; final String dateFormatted = time.format(DateTimeFormatter.ISO_TIME);

response.setBody("current time is " + dateFormatted);


HttpMate also provides a simple Java HttpServer wrapper - PureJavaEndpoint - that would allow you to start an endpoint without any further dependency.

All we need to do is give it the instance of the HttpMate:

package com.envimate.examples.http;

import com.envimate.httpmate.HttpMate; import com.envimate.httpmate.convenience.endpoints.PureJavaEndpoint; import com.envimate.httpmate.convenience.handler.HttpHandler;

import java.time.LocalDateTime; import java.time.format.DateTimeFormatter;

import static com.envimate.httpmate.HttpMate.anHttpMateConfiguredAs; import static com.envimate.httpmate.LowLevelBuilder.LOW_LEVEL;

public final class Application { public static void main(String[] args) { final HttpHandler httpHandler = (request, response) -> { final LocalDateTime time =; final String dateFormatted = time.format(DateTimeFormatter.ISO_TIME);

        response.setBody("current time is " + dateFormatted);

    final HttpMate httpMate = anHttpMateConfiguredAs(LOW_LEVEL)
            .get("/time", httpHandler)


Notice that we have configured our httpHandler to serve the path /time, when invoked with method GET.

It's time to start our application and make some requests:

curl http://localhost:1337/time
current time is 15:09:34.458756

Before we put this all into a Dockerfile, we need to package it as a good-old jar.

Building the Jar

We'd need two maven plugins for that: maven-compiler-plugin and maven-assembly-plugin to build the executable jar.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="" xmlns=""





Once we have that, let's build our jar:

mvn clean verify

And run the resulting jar:

java -jar target/simple-java-http-docker-0.0.1-jar-with-dependencies.jar

Same curl:

curl http://localhost:1337/time
current time is 15:14:42.992563

Dockerizing the Jar

The Dockerfile looks quite simple:

ROM openjdk:12

ADD target/simple-java-http-docker-0.0.1-jar-with-dependencies.jar /opt/application.jar


ENTRYPOINT exec java -jar /opt/application.jar

It specifies

  • FROM: which image to use as a base. We will use an openjdk image.
  • ADD: the jar we want to the directory we want.
  • EXPOSE: the port we are listening on.
  • ENTRYPOINT: to the command, we want to execute.

To build and tag our Docker image, we run the following command from the root of the directory:

docker build --tag simple-java-http-docker .

This will produce a docker image that we can run:

docker run --publish 1337:1337 simple-java-http-docker

Notice that we are passing the --publish parameter, which indicates that the exposed 1337 port, is available under the 1337 port of the machine.

Same curl:

curl http://localhost:1337/time
current time is 15:23:04.275515

And that is it: we have our simple HTTP endpoint dockerized!

The Icing

Of course, this is a simplified example, and the endpoint we wrote is not entirely useful. It demonstrates, though, that you don't need tons of libraries just to have a running HTTP endpoint, how easy it is to package a runnable jar, use Docker with your Java application, and the basic usage of the low-level HttpMate.

This kind of two-minute setup can be handy when you need to quickly spin a test HTTP server. The other day I was working on a Twitter-bot (stay tuned for an article about that) and I had to debug what my request really looks like on the receiving side. Obviously, I couldn't ask Twitter to give me a dump of my request, so I needed a simple endpoint, that would output everything possible about my request.

HttpMate's handler provides access to an object called MetaData which is pretty much what it's called — the meta-data of your request, meaning everything available about your request.

Using that object, we can print everything there is to the request.

public final class FakeTwitter {
    public static void main(String[] args) {
        final HttpMate httpMate = HttpMate.aLowLevelHttpMate()
                .callingTheHandler(metaData -> {
                .forRequestPath("/*").andRequestMethods(GET, POST, PUT)



The request path /time is now replaced with a pattern, capturing all paths, and we can add all the HTTP methods we are interested in.

Running our FakeTwitter server and issuing request:

curl -XGET http://localhost:1337/some/path/with?someParameter=someValue

We'll see the following output in the console (output formatted for readability: it is a map underneath, so you can format it nicely if you so wish)

    [email protected],
    LOGGER=com.envimate.httpmate.logger.Loggers$Lambda$17/[email protected],
    HANDLER=com.envimate.examples.http.FakeTwitter$Lambda$18/[email protected]

Thanks for reading. If you liked this post, share it with all of your programming buddies!

Further reading

☞ Build a Basic App with Spring Boot and JPA using PostgreSQL

☞ Build a Simple CRUD App with Spring Boot and Vue.js

☞ Introducing TensorFlow.js: Machine Learning in Javascript

☞ An illustrated guide to Kubernetes Networking

☞ Google Kubernetes Engine By Example

☞ AWS DevOps: Introduction to DevOps on AWS

☞ Docker Tutorial for Beginners

☞ Kotlin Microservices With Micronaut, Spring Cloud, and JPA

Originally published on

docker web-development spring-boot java

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Top 10 Advanced Java and Spring Boot Courses for Full-Stack Java Developers

These are best online courses to learn Spring Boot, Advanced Java, Docker, React, Microservices, DEvops, and Angular to become full stack Java developer.

Productive Web Development in Java with IntelliJ IDEA, Spring Boot and Vaadin

We cover tips and tricks to help you become a more productive web app developer. We build a small web app using Vaadin's Java API and Spring Boot. We show you how to set up IntelliJ IDEA to automatically reload your app and browser as you develop and cover our favorite shortcuts and hacks for productive and enjoyable web app development in Java.

Hire Web Developer

Looking for an attractive & user-friendly web developer?, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java Spring Boot First App

Step by Step to your First Spring App