<dependency> <groupId>com.typesafe.akka</groupId> <artifactId>akka-http_2.13</artifactId> <version>10.2.4</version> </dependency> import akka.actor.ActorSystem; import akka.http.javadsl.Http; import akka.http.javadsl.ServerBinding; import akka.http.javadsl.marshallers.jackson.Jackson; import akka.http.javadsl.model.HttpEntities; import akka.http.javadsl.model.HttpRequest; import akka.http.javadsl.model.HttpResponse; import akka.http.javadsl.server.Route; import akka.http.javadsl.server.AllDirectives; import java.util.concurrent.CompletionStage; public class MyApp extends AllDirectives { public static void main(String[] args) { ActorSystem system = ActorSystem.create("my-app"); Http http = Http.get(system); MyApp app = new MyApp(); final Route route = app.createRoute(); final CompletionStage<ServerBinding> binding = http.newServerAt("localhost", 8080).bind(route); System.out.println("Server online at http://localhost:8080/"); } private Route createRoute() { return path("hello", () -> get(() -> complete(HttpResponse.create() .withEntity(HttpEntities.create( Jackson.marshaller().toEntity(new Greeting("Hello, World!")) )) ) ) ); } static class Greeting { public String message; public Greeting(String message) { this.message = message; } } }


上一篇:
下一篇:
切换中文