Skip to content
http4j is an HTTP toolkit written in Java (in the vein of http4k) that allows for functional HTTP services
Java
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
clients minor fixes Jun 1, 2018
core-test added start of clients and moved around modules Jan 24, 2018
core minor fixes Jun 1, 2018
examples
servers
.gitignore
LICENSE Create LICENSE Jan 8, 2018
README.md
pom.xml

README.md

seeking others to work with on the project

http4j

http4j is an HTTP toolkit written in Java8 that allows for functional HTTP services -- in similar vein to http4k however geared for Java codebases.

Functional

http4j follows Twitter's Your Server as a Function whitepaper, and handlers are simple functional interfaces. To get started in http4j all you need to know the following two interfaces. (they are in fact the same interface for the clients as well)

@FunctionalInterface
public interface HttpHandler {

  HttpResponse handle(HttpRequest request);
}

@FunctionalInterface
public interface HttpFilter {

  HttpHandler handle(HttpHandler handler);
}

Example

public class ServerWithSimpleRouter {

  private static HttpResponse echo(HttpRequest request) {
    String message = request.params().get("message");
    return HttpResponse.status(200).body(message);
  }

  public static void main(String[] args) {
    Router router =
        Router.builder()
            .get("/ping", HttpHandlers.pong())
            .get("/echo/{message}", ServerWithSimpleRouter::echo)
            .build();
    int port = 8888;
    HttpServer server = new SunHttpServerCreator(port).create(router);
    server.start();
    
    HttpHandler client = new JdkClient();
    HttpResponse response = client.handle(HttpRequest.get((String.format("http://localhost:%s/ping", port))));
  }
}

There is even an in-memory transport to make testing simple

public class InMemoryServerWithSimpleRouter {
  public static void main(String[] args) {
    Router router =
        Router.builder()
            .get("/ping", HttpHandlers.pong())
            .get("/echo/{message}", ServerWithSimpleRouter::echo)
            .build();
    InMemoryServer server = new InMemoryServerCreator().create(router);
    server.start();
    
    //the same functional interface, however goes directly to the handler
    //without the underlying HTTP transport layer
    HttpHandler client = server.getClient();
    HttpResponse response = client.handle(HttpRequest.get("http://localhost:%s/ping"));
  }
}

TODO

  1. Additional server creators
  2. More tests
  3. Github CICD
You can’t perform that action at this time.