Skip to content
Branch: master
Find file History
#255 Compare This branch is 203 commits ahead, 224 commits behind 1.x.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Apollo HTTP Service

Maven Central

The apollo-http-service library is a small bundle of Apollo modules. It incorporates both apollo-api and apollo-core and ties them together with the jetty-http-server and the okhttp-client to make a complete service. It also adds logback-classic as an SLF4J implementation to give you logging capabilities.

Apollo HTTP Service gives you what you need to start your backend service. Here, for example, we tell HttpService to boot a service named "ping", defined by the function Ping::init, and handle any command line arguments passed in through the args variable:

public static void main(String... args) throws LoadingException {
  HttpService.boot(Ping::init, "ping", args);

The HttpService class is a good example of how apollo-core together with apollo-api and other modules come together to build a fully functional service. You can find documentation for each module in its respective directory under /modules.

Minimal project skeleton

├── pom.xml
└── src/
    └── main/
        ├── java/
        │   └── com/
        │       └── example/
        │           └──
        └── resources/
            └── ping.conf


package com.example;

import com.spotify.apollo.AppInit;
import com.spotify.apollo.Environment;
import com.spotify.apollo.route.Route;
import com.spotify.apollo.httpservice.LoadingException;
import com.spotify.apollo.httpservice.HttpService;

public final class Ping {

   * The main entry point of the java process which will delegate to
   * {@link HttpService#boot(AppInit, String, String...)}.
   * @param args  program arguments passed in from the command line
   * @throws LoadingException if anything goes wrong during the service boot sequence
  public static void main(String... args) throws LoadingException {
    HttpService.boot(Ping::init, "ping", args);

   * An implementation of the {@link AppInit} functional interface which simply sets up a
   * "hello world" handler on the root route "/".
   * @param environment  The Apollo {@link Environment} that the service is in.
  static void init(Environment environment) {
        .registerAutoRoute(Route.sync("GET", "/ping", requestContext -> "pong"));


# Configuration for http interface
http.server.port = 8080
http.server.port = ${?HTTP_PORT}

For more information on how to manage configuration, see Apollo Core, the logback-classic and the Typesafe Config documentation.




    <name>Simple Ping Service</name>





The Maven configuration is a bit lengthy so it deserves some more explanation:

  • We use a property named mainClass to refer to our main class. This will be used later.
  • Under the dependencyManagement we import all the Apollo artifact versions through the apollo-bom artifact. For more information about importing managed dependencies, see the Maven documentation.
  • We configure the compiler plugin to target JDK 8.
  • We configure the maven-dependency-plugin to copy all runtime dependency jars into ${}/lib. These will be referenced from the main artifact.
  • We configure maven-jar-plugin to add the classpath jars to the manifest, prefixed with lib/ along with the MainClass entry to use our main class.

Compile and Run

mvn package
java -jar target/ping.jar

Try a request with curl

$ curl http://localhost:8080/ping


The HTTP service logs incoming requests and their responses, using a default implementation that logs with approximately the Apache HTTPD 'combined' format.

To send this to an access log file, use a configuration similar to:

    <appender name="ACCESSLOG" class="ch.qos.logback.core.FileAppender">

    <logger name="com.spotify.apollo.http.server.CombinedFormatLogger" level="INFO">
        <appender-ref ref="ACCESSLOG"/>

To customise the logging implementation using Guice optional injections, do something along the lines of this:

public class MyModule extends AbstractModule {
  // ...
  protected void configure() {
    bind(com.spotify.apollo.http.server.RequestOutcomeConsumer.class).toInstance(new MyLogger());
You can’t perform that action at this time.