diff --git a/akka-docs/rst/scala/http/migration-from-old-http-javadsl.rst b/akka-docs/rst/scala/http/migration-from-old-http-javadsl.rst index e9bde1935d..8ce46f53a2 100644 --- a/akka-docs/rst/scala/http/migration-from-old-http-javadsl.rst +++ b/akka-docs/rst/scala/http/migration-from-old-http-javadsl.rst @@ -54,6 +54,13 @@ pattern from blogs which used Scala, yet need to apply it in Java and the other It is now possible to implement marshallers using Java. Refer to :ref:`marshalling-java` for details. +Some complete* overloads changed to completeOK* +----------------------------------------------- +In JavaDSL when complete is called with only an entity, the ``OK`` response code is *assumed*, +to make this more explicit these methods contain the word ``OK`` in them. + +This has been made more consistent than previously, across all overloads and Future-versions of these APIs. + Migration help -------------- As always, feel free to reach out via the `akka-user `_ mailing list or gitter channels, diff --git a/akka-http-tests/src/main/java/akka/http/javadsl/server/examples/petstore/PetStoreExample.java b/akka-http-tests/src/main/java/akka/http/javadsl/server/examples/petstore/PetStoreExample.java index 3d7c13b96e..58a10527b1 100644 --- a/akka-http-tests/src/main/java/akka/http/javadsl/server/examples/petstore/PetStoreExample.java +++ b/akka-http-tests/src/main/java/akka/http/javadsl/server/examples/petstore/PetStoreExample.java @@ -4,38 +4,35 @@ package akka.http.javadsl.server.examples.petstore; -import static akka.http.javadsl.server.Directives.complete; -import static akka.http.javadsl.server.Directives.delete; -import static akka.http.javadsl.server.Directives.entity; -import static akka.http.javadsl.server.Directives.get; -import static akka.http.javadsl.server.Directives.getFromResource; -import static akka.http.javadsl.server.Directives.path; -import static akka.http.javadsl.server.Directives.pathPrefix; -import static akka.http.javadsl.server.Directives.put; -import static akka.http.javadsl.server.Directives.reject; -import static akka.http.javadsl.server.Directives.route; -import static akka.http.javadsl.server.StringUnmarshallers.INTEGER; - -import java.io.IOException; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Function; - import akka.actor.ActorSystem; import akka.http.javadsl.ConnectHttp; import akka.http.javadsl.Http; import akka.http.javadsl.marshallers.jackson.Jackson; import akka.http.javadsl.model.StatusCodes; import akka.http.javadsl.server.Route; -import akka.http.javadsl.server.examples.simple.SimpleServerApp; import akka.stream.ActorMaterializer; +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; + +import static akka.http.javadsl.server.Directives.*; +import static akka.http.javadsl.server.StringUnmarshallers.INTEGER; + public class PetStoreExample { private static Route putPetHandler(Map pets, Pet thePet) { pets.put(thePet.getId(), thePet); return complete(StatusCodes.OK, thePet, Jackson.marshaller()); } + + private static Route alternativeFuturePutPetHandler(Map pets, Pet thePet) { + pets.put(thePet.getId(), thePet); + CompletableFuture futurePet = CompletableFuture.supplyAsync(() -> thePet); + return completeOKWithFuture(futurePet, Jackson.marshaller()); + } public static Route appRoute(final Map pets) { PetStoreController controller = new PetStoreController(pets); @@ -65,6 +62,14 @@ public class PetStoreExample { putPetHandler(pets, thePet) ) ), + // 2.1. using a method, and internally handling a Future value + path("alternate", () -> + put(() -> + entity(Jackson.unmarshaller(Pet.class), thePet -> + putPetHandler(pets, thePet) + ) + ) + ), // 3. calling a method of a controller instance delete(() -> controller.deletePet(petId)) @@ -90,4 +95,4 @@ public class PetStoreExample { System.console().readLine("Type RETURN to exit..."); system.terminate(); } -} \ No newline at end of file +} diff --git a/akka-http-tests/src/test/java/akka/http/javadsl/server/JavaTestServer.java b/akka-http-tests/src/test/java/akka/http/javadsl/server/JavaTestServer.java index 171d5c5cdb..4ccb14c1e5 100644 --- a/akka-http-tests/src/test/java/akka/http/javadsl/server/JavaTestServer.java +++ b/akka-http-tests/src/test/java/akka/http/javadsl/server/JavaTestServer.java @@ -48,8 +48,8 @@ public class JavaTestServer extends AllDirectives { // or import static Directiv ); final Route crash = path("crash", () -> - path("scala", () -> completeWithFutureString(akka.dispatch.Futures.failed(new Exception("Boom!")))).orElse( - path("java", () -> completeWithFutureString(CompletableFuture.supplyAsync(() -> { throw new RuntimeException("Boom!"); })))) + path("scala", () -> completeOKWithFutureString(akka.dispatch.Futures.failed(new Exception("Boom!")))).orElse( + path("java", () -> completeOKWithFutureString(CompletableFuture.supplyAsync(() -> { throw new RuntimeException("Boom!"); })))) ); final Route inner = path("inner", () -> @@ -119,4 +119,4 @@ public class JavaTestServer extends AllDirectives { // or import static Directiv } }); } -} \ No newline at end of file +} diff --git a/akka-http/src/main/scala/akka/http/javadsl/server/directives/RouteDirectives.scala b/akka-http/src/main/scala/akka/http/javadsl/server/directives/RouteDirectives.scala index 6e8f5ae227..91f4a8b4dc 100644 --- a/akka-http/src/main/scala/akka/http/javadsl/server/directives/RouteDirectives.scala +++ b/akka-http/src/main/scala/akka/http/javadsl/server/directives/RouteDirectives.scala @@ -153,6 +153,7 @@ abstract class RouteDirectives extends RespondWithDirectives { /** * Completes the request as HTTP 200 OK, marshalling the given value as response entity. */ + @CorrespondsTo("complete") def completeOK[T](value: T, marshaller: Marshaller[T, RequestEntity]) = RouteAdapter { D.complete(ToResponseMarshallable(value)(fromToEntityMarshaller()(marshaller))) } @@ -178,7 +179,7 @@ abstract class RouteDirectives extends RespondWithDirectives { * Completes the request by marshalling the given future value into an http response. */ @CorrespondsTo("complete") - def completeWithFutureString(value: scala.concurrent.Future[String]) = RouteAdapter { + def completeOKWithFutureString(value: scala.concurrent.Future[String]) = RouteAdapter { D.complete(value) } @@ -190,6 +191,14 @@ abstract class RouteDirectives extends RespondWithDirectives { D.complete(status.fast.map(_.asScala)) } + /** + * Completes the request by marshalling the given value into an http response. + */ + @CorrespondsTo("complete") + def completeOKWithFuture[T](value: scala.concurrent.Future[T], marshaller: Marshaller[T, RequestEntity]) = RouteAdapter { + D.complete(value.fast.map(v ⇒ ToResponseMarshallable(v)(fromToEntityMarshaller()(marshaller)))) + } + /** * Completes the request by marshalling the given value into an http response. */ @@ -204,7 +213,7 @@ abstract class RouteDirectives extends RespondWithDirectives { * Completes the request by marshalling the given future value into an http response. */ @CorrespondsTo("complete") - def completeWithFutureResponse(value: CompletionStage[HttpResponse]) = RouteAdapter { + def completeWithFuture(value: CompletionStage[HttpResponse]) = RouteAdapter { D.complete(value.asScala.fast.map(_.asScala)) } @@ -212,7 +221,15 @@ abstract class RouteDirectives extends RespondWithDirectives { * Completes the request by marshalling the given future value into an http response. */ @CorrespondsTo("complete") - def completeWithFutureString(value: CompletionStage[String]) = RouteAdapter { + def completeOKWithFuture(value: CompletionStage[RequestEntity]) = RouteAdapter { + D.complete(value.asScala.fast.map(_.asScala)) + } + + /** + * Completes the request by marshalling the given future value into an http response. + */ + @CorrespondsTo("complete") + def completeOKWithFutureString(value: CompletionStage[String]) = RouteAdapter { D.complete(value.asScala) } @@ -224,6 +241,14 @@ abstract class RouteDirectives extends RespondWithDirectives { D.complete(status.asScala.fast.map(_.asScala)) } + /** + * Completes the request with an `OK` status code by marshalling the given value into an http response. + */ + @CorrespondsTo("complete") + def completeOKWithFuture[T](value: CompletionStage[T], marshaller: Marshaller[T, RequestEntity]) = RouteAdapter { + D.complete(value.asScala.fast.map(v ⇒ ToResponseMarshallable(v)(fromToEntityMarshaller()(marshaller)))) + } + /** * Completes the request by marshalling the given value into an http response. */