VERB PATH CONTROLLER_METHOD GET / controllers.Application.index() GET /foo controllers.Application.foo()
Declarative, type-safe URL Mapping
public static Result index() { return ok("Hello World!"); }
import play.libs.F.*; private static Integer calc() { return (5134 * 5789) / 349; } public static Promise<Result> basicPromise() { Promise<Integer> promise = Promise.promise(() -> calc()); return promise.map(i -> ok("Result is: " + i)); }
public static Promise<Result> getPage() { final Promise<WSResponse> promise = WS.url("http://google.com").get(); return promise.map(response -> ok(response.getBody())); }- Fully async and non-blocking - Client to Server, Server to Service
public static Promise<Result> composition() { final Promise<WSResponse> googlePromise = WS.url("http://google.com").get(); final Promise<WSResponse> twitterPromise = WS.url("http://twitter.com").get(); return googlePromise.flatMap(google -> twitterPromise.map(twitter -> ok(google.getBody() + twitter.getBody()))); }- Three requests, two in parallel (depending on thread availability) - All async & non-blocking
Akka decouples communication from failure handling: