-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
!htp akka#17965 more consistent and 22-ified javadsl handleWith
- Loading branch information
Showing
25 changed files
with
364 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
akka-docs-dev/rst/java/code/docs/http/javadsl/LambdaRoutingExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com> | ||
*/ | ||
|
||
package docs.http.javadsl; | ||
|
||
import akka.actor.ActorSystem; | ||
import akka.dispatch.Futures; | ||
import akka.http.javadsl.server.*; | ||
import akka.http.javadsl.server.values.Parameter; | ||
import akka.http.javadsl.server.values.Parameters; | ||
import akka.http.javadsl.server.values.PathMatcher; | ||
import akka.http.javadsl.server.values.PathMatchers; | ||
import scala.concurrent.Future; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.Callable; | ||
|
||
public class LambdaRoutingExample extends HttpApp { | ||
static Parameter<Integer> x = Parameters.intValue("x"); | ||
static Parameter<Integer> y = Parameters.intValue("y"); | ||
|
||
static PathMatcher<Integer> xSegment = PathMatchers.integerNumber(); | ||
static PathMatcher<Integer> ySegment = PathMatchers.integerNumber(); | ||
|
||
public static RouteResult multiply(RequestContext ctx, int x, int y) { | ||
int result = x * y; | ||
return ctx.complete(String.format("%d * %d = %d", x, y, result)); | ||
} | ||
|
||
public static Future<RouteResult> multiplyAsync(final RequestContext ctx, final int x, final int y) { | ||
return Futures.future(() -> multiply(ctx, x, y), ctx.executionContext()); | ||
} | ||
|
||
@Override | ||
public Route createRoute() { | ||
return | ||
route( | ||
// matches the empty path | ||
pathSingleSlash().route( | ||
getFromResource("web/calculator.html") | ||
), | ||
// matches paths like this: /add?x=42&y=23 | ||
path("add").route( | ||
handleWith2(x, y, LambdaRoutingExample::multiply) | ||
), | ||
path("subtract").route( | ||
handleWith2(x, y, this::subtract) | ||
), | ||
// matches paths like this: /multiply/{x}/{y} | ||
path("multiply", xSegment, ySegment).route( | ||
// bind handler by reflection | ||
handleWith2(xSegment, ySegment, LambdaRoutingExample::multiply) | ||
), | ||
path("multiplyAsync", xSegment, ySegment).route( | ||
// bind async handler | ||
handleWithAsync2(xSegment, ySegment, LambdaRoutingExample::multiplyAsync) | ||
) | ||
); | ||
} | ||
|
||
public RouteResult add(RequestContext ctx, Integer xVal, Integer yVal) { | ||
int result = xVal + yVal; | ||
return ctx.complete(String.format("%d + %d = %d", xVal, yVal, result)); | ||
} | ||
|
||
public RouteResult subtract(RequestContext ctx, Integer xVal, Integer yVal) { | ||
int result = xVal - yVal; | ||
return ctx.complete(String.format("%d - %d = %d", xVal, yVal, result)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.