This commit is contained in:
parent
31de5952b0
commit
a299b30c0b
4 changed files with 62 additions and 25 deletions
|
|
@ -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.
|
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
|
Migration help
|
||||||
--------------
|
--------------
|
||||||
As always, feel free to reach out via the `akka-user <https://groups.google.com/forum/#!searchin/akka-user/>`_ mailing list or gitter channels,
|
As always, feel free to reach out via the `akka-user <https://groups.google.com/forum/#!searchin/akka-user/>`_ mailing list or gitter channels,
|
||||||
|
|
|
||||||
|
|
@ -4,38 +4,35 @@
|
||||||
|
|
||||||
package akka.http.javadsl.server.examples.petstore;
|
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.actor.ActorSystem;
|
||||||
import akka.http.javadsl.ConnectHttp;
|
import akka.http.javadsl.ConnectHttp;
|
||||||
import akka.http.javadsl.Http;
|
import akka.http.javadsl.Http;
|
||||||
import akka.http.javadsl.marshallers.jackson.Jackson;
|
import akka.http.javadsl.marshallers.jackson.Jackson;
|
||||||
import akka.http.javadsl.model.StatusCodes;
|
import akka.http.javadsl.model.StatusCodes;
|
||||||
import akka.http.javadsl.server.Route;
|
import akka.http.javadsl.server.Route;
|
||||||
import akka.http.javadsl.server.examples.simple.SimpleServerApp;
|
|
||||||
import akka.stream.ActorMaterializer;
|
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 {
|
public class PetStoreExample {
|
||||||
|
|
||||||
private static Route putPetHandler(Map<Integer, Pet> pets, Pet thePet) {
|
private static Route putPetHandler(Map<Integer, Pet> pets, Pet thePet) {
|
||||||
pets.put(thePet.getId(), thePet);
|
pets.put(thePet.getId(), thePet);
|
||||||
return complete(StatusCodes.OK, thePet, Jackson.<Pet>marshaller());
|
return complete(StatusCodes.OK, thePet, Jackson.<Pet>marshaller());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Route alternativeFuturePutPetHandler(Map<Integer, Pet> pets, Pet thePet) {
|
||||||
|
pets.put(thePet.getId(), thePet);
|
||||||
|
CompletableFuture<Pet> futurePet = CompletableFuture.supplyAsync(() -> thePet);
|
||||||
|
return completeOKWithFuture(futurePet, Jackson.<Pet>marshaller());
|
||||||
|
}
|
||||||
|
|
||||||
public static Route appRoute(final Map<Integer, Pet> pets) {
|
public static Route appRoute(final Map<Integer, Pet> pets) {
|
||||||
PetStoreController controller = new PetStoreController(pets);
|
PetStoreController controller = new PetStoreController(pets);
|
||||||
|
|
@ -65,6 +62,14 @@ public class PetStoreExample {
|
||||||
putPetHandler(pets, thePet)
|
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
|
// 3. calling a method of a controller instance
|
||||||
delete(() -> controller.deletePet(petId))
|
delete(() -> controller.deletePet(petId))
|
||||||
|
|
@ -90,4 +95,4 @@ public class PetStoreExample {
|
||||||
System.console().readLine("Type RETURN to exit...");
|
System.console().readLine("Type RETURN to exit...");
|
||||||
system.terminate();
|
system.terminate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,8 +48,8 @@ public class JavaTestServer extends AllDirectives { // or import static Directiv
|
||||||
);
|
);
|
||||||
|
|
||||||
final Route crash = path("crash", () ->
|
final Route crash = path("crash", () ->
|
||||||
path("scala", () -> completeWithFutureString(akka.dispatch.Futures.<String>failed(new Exception("Boom!")))).orElse(
|
path("scala", () -> completeOKWithFutureString(akka.dispatch.Futures.<String>failed(new Exception("Boom!")))).orElse(
|
||||||
path("java", () -> completeWithFutureString(CompletableFuture.<String>supplyAsync(() -> { throw new RuntimeException("Boom!"); }))))
|
path("java", () -> completeOKWithFutureString(CompletableFuture.<String>supplyAsync(() -> { throw new RuntimeException("Boom!"); }))))
|
||||||
);
|
);
|
||||||
|
|
||||||
final Route inner = path("inner", () ->
|
final Route inner = path("inner", () ->
|
||||||
|
|
@ -119,4 +119,4 @@ public class JavaTestServer extends AllDirectives { // or import static Directiv
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -153,6 +153,7 @@ abstract class RouteDirectives extends RespondWithDirectives {
|
||||||
/**
|
/**
|
||||||
* Completes the request as HTTP 200 OK, marshalling the given value as response entity.
|
* 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 {
|
def completeOK[T](value: T, marshaller: Marshaller[T, RequestEntity]) = RouteAdapter {
|
||||||
D.complete(ToResponseMarshallable(value)(fromToEntityMarshaller()(marshaller)))
|
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.
|
* Completes the request by marshalling the given future value into an http response.
|
||||||
*/
|
*/
|
||||||
@CorrespondsTo("complete")
|
@CorrespondsTo("complete")
|
||||||
def completeWithFutureString(value: scala.concurrent.Future[String]) = RouteAdapter {
|
def completeOKWithFutureString(value: scala.concurrent.Future[String]) = RouteAdapter {
|
||||||
D.complete(value)
|
D.complete(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -190,6 +191,14 @@ abstract class RouteDirectives extends RespondWithDirectives {
|
||||||
D.complete(status.fast.map(_.asScala))
|
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.
|
* 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.
|
* Completes the request by marshalling the given future value into an http response.
|
||||||
*/
|
*/
|
||||||
@CorrespondsTo("complete")
|
@CorrespondsTo("complete")
|
||||||
def completeWithFutureResponse(value: CompletionStage[HttpResponse]) = RouteAdapter {
|
def completeWithFuture(value: CompletionStage[HttpResponse]) = RouteAdapter {
|
||||||
D.complete(value.asScala.fast.map(_.asScala))
|
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.
|
* Completes the request by marshalling the given future value into an http response.
|
||||||
*/
|
*/
|
||||||
@CorrespondsTo("complete")
|
@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)
|
D.complete(value.asScala)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -224,6 +241,14 @@ abstract class RouteDirectives extends RespondWithDirectives {
|
||||||
D.complete(status.asScala.fast.map(_.asScala))
|
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.
|
* Completes the request by marshalling the given value into an http response.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue