Skip to main content

Getting Started

ActiveJ is a minimalistic and boilerplate-free Java platform that is intended to replace Spring, Spark, Micronaut, and other similar solutions. It features a rich stack of technologies with an efficient async programming model and powerful DI library ActiveJ Inject.

ActiveJ consists of several loosely coupled components that complement and empower each other: async core, efficient I/O processing, high-performance Jetty and Netty alternatives, along with handy boot utilities.

In this tutorial we will create a simple HTTP server that sends a “Hello World!” greeting. With ActiveJ Launchers, particularly HttpServerLauncher subclass, and ActiveJ Inject DI you can write a fully-functional server in around 10 lines of code.

What you will need:#

Which ActiveJ technologies will be used:#

To proceed with this guide you have three options:#

Create from Maven Archetype#

Simply enter the following command in the terminal:

mvn archetype:generate -DarchetypeGroupId=io.activej -DarchetypeArtifactId=archetype-http -DarchetypeVersion=5.0-beta2

The project will be automatically generated on your machine. Open WebApp and run its main method. Then open your favourite browser and go to localhost:8080

Working Example#

Clone ActiveJ locally and import it as a Maven project. Check out branch. Before running the example, build the project (Ctrl + F9 for IntelliJ IDEA).

Then open HttpHelloWorldExample class, which is located at activej/examples/tutorials/getting-started and run its main method. Open your favourite browser and go to localhost:8080

Step-by-step guide#

1. Configure the project#

First, create a folder for application and build an appropriate project structure:

getting-started└── pom.xml└── src    └── main        └── java            └──

Next, you need to integrate ActiveJ in your project. There are two ways to do it. The first option is to add Maven dependency to your project. Simply configure your pom.xml file in the following way:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="" xmlns:xsi="" xsi:schemaLocation="">  <modelVersion>4.0.0</modelVersion>
  <groupId>io.activej</groupId>  <artifactId>tutorial-getting-started</artifactId>  <version>5.0-SNAPSHOT</version>
  <name>Tutorials : Getting-Started</name>
  <properties>    <maven.compiler.source>1.8</maven.compiler.source>    <>1.8</>  </properties>
  <dependencies>    <dependency>      <groupId>io.activej</groupId>      <artifactId>activej-launchers-http</artifactId>      <version>${project.version}</version>    </dependency>    <dependency>      <groupId>ch.qos.logback</groupId>      <artifactId>logback-classic</artifactId>      <version>1.2.3</version>    </dependency>  </dependencies></project>

The second option is to clone ActiveJ from GitHub, import it as a Maven project, check out branch v5.0-beta2 and run the following script in project directory:


Next, configure your pom.xml file like in the example above. Make sure that your project SDK is set to 1.8+.

2. Write HttpHelloWorldExample class#

After you integrate ActiveJ in your project in one of the suggested ways, write down the following code to

public final class HttpHelloWorldExample extends HttpServerLauncher {  @Provides  AsyncServlet servlet() {    return request -> HttpResponse.ok200().withPlainText("Hello World");  }
  public static void main(String[] args) throws Exception {    Launcher launcher = new HttpHelloWorldExample();    launcher.launch(args);  }}

First, we extend HttpHelloWorldExample from HttpServerLauncher to manage application lifecycle. In this example, the only important thing to know about the superclass is that it implements the launch method that starts our server.

Next, we provide an AsyncServlet which receives HttpRequests from clients, creates HttpResponses and sends them. @Provides annotation means that this method is available for binding as the root HTTP endpoint listener.

To define processing of the received requests, we override AsyncServlet.serve method with a lambda. We create a promise of HttpResponse with code 200 and "Hello World!" body. ActiveJ is fully asynchronous, so our HTTP Servlets are asynchronous too. A simple HttpResponse can be replaced with a Promise of HttpResponse to make it asynchronous.

Finally, we define main method and launch our server with launch method. This method injects dependencies, starts the application, runs it and stops it when needed.

3. Run the app#

Now, let's test the application. First, run HttpHelloWorldExample.main, then open your favourite browser and go to localhost:8080 You will receive a Hello World! message processed by the server. Congratulations, you've just created your first ActiveJ application!

What's next?#

To make ActiveJ more developer-friendly, we've created dozens of tutorials and examples of different scales, representing most of the platform's capabilities. Click "Next" to get to the next tutorial. You can also explore ActiveJ docs first.