public static void main(String[] args) { Vertx vertx = Vertx.vertx(); Router router = Router.router(vertx); router.get("/").handler(rc -> rc.response().end("Hello")); router.get("/:name").handler(rc -> rc.response().end("Hello " + rc.pathParam("name"))); vertx.createHttpServer() .requestHandler(router) .listen(8080); } }
public static void main(String[] args) { // Create the Vert.x instance Vertx vertx = Vertx.vertx(); // Create a router Router router = Router.router(vertx); router.get("/").handler(rc -> rc.response().end("Hello")); router.get("/:name").handler(rc -> rc.response().end("Hello " + rc.pathParam("name"))); // Start the HTTP server. vertx.createHttpServer() .requestHandler(router) .listen(8080); } }
@Override public void start() throws Exception { final Image image = new Image(vertx, "coin.png"); Router router = Router.router(vertx); router.get("/").handler(ctx -> { ctx.response() .putHeader("Content-Type", "text/html") .end(image.generateHTML(16)); }); router.get("/img/:x/:y").handler(ctx -> { ctx.response() .putHeader("Content-Type", "image/png") .end(image.getPixel(Integer.parseInt(ctx.pathParam("x")), Integer.parseInt(ctx.pathParam("y")))); }); vertx.createHttpServer( new HttpServerOptions() .setSsl(true) .setUseAlpn(true) .setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("tls/server-key.pem").setCertPath("tls/server-cert.pem"))).requestHandler(router) .listen(8443); } }
@Override public @Nullable String pathParam(String name) { return decoratedContext.pathParam(name); }
@Override public @Nullable String pathParam(String name) { return inner.pathParam(name); }
@Test public void testRegexGroupsNamesWithMethodOverride() throws Exception { List<String> groupNames = new ArrayList<>(); groupNames.add("FirstParam"); groupNames.add("SecondParam"); Route route = router.getWithRegex("\\/([a-z]{2})([a-z]{2})"); route.setRegexGroupsNames(groupNames); route.handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("FirstParam") + "-" + routingContext.pathParam("SecondParam")) .end()); testRequest(HttpMethod.GET, "/aabb", 200, "aa-bb"); }
@Test public void testSetRegexGroupsNamesMethodWithUnorderedGroups() throws Exception { List<String> groupNames = new ArrayList<>(); groupNames.add("firstParam"); groupNames.add("secondParam"); Route route1 = router.getWithRegex("\\/(?<p1>[a-z]{2})(?<p0>[a-z]{2})"); route1.setRegexGroupsNames(groupNames); route1.handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam")) .end()); testRequest(HttpMethod.GET, "/bbaa", 200, "aa-bb"); }
@Test public void testSetRegexGroupsNamesMethodWithNestedRegex() throws Exception { List<String> groupNames = new ArrayList<>(); groupNames.add("firstParam"); groupNames.add("secondParam"); Route route1 = router.getWithRegex("\\/(?<p1>[a-z]{2}(?<p0>[a-z]{2}))"); route1.setRegexGroupsNames(groupNames); route1.handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam")) .end()); testRequest(HttpMethod.GET, "/bbaa", 200, "aa-bbaa"); }
@Test public void testRegexGroupsNames() throws Exception { router.getWithRegex("\\/(?<firstParam>[a-z]{2})(?<secondParam>[a-z]{2})").handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam")) .end()); testRequest(HttpMethod.GET, "/aabb", 200, "aa-bb"); }
@Test public void testRegexGroupsNamesWithNestedGroups() throws Exception { router.getWithRegex("\\/(?<secondParam>[a-z]{2}(?<firstParam>[a-z]{2}))").handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam")) .end()); testRequest(HttpMethod.GET, "/bbaa", 200, "aa-bbaa"); }
@Test public void testSetRegexGroupsNamesMethod() throws Exception { List<String> groupNames = new ArrayList<>(); groupNames.add("hello"); Route route1 = router.getWithRegex("\\/(?<p0>[a-z]{2})"); route1.setRegexGroupsNames(groupNames); route1.handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("hello")) .end()); testRequest(HttpMethod.GET, "/hi", 200, "hi"); }
/** * Gets the value of a single path parameter * @param name the name of parameter as defined in path declaration * @return the actual value of the parameter or null if it doesn't exist */ public String pathParam(String name) { String ret = delegate.pathParam(name); return ret; }
@Test public void testPathParamsWithReroute() throws Exception { String paramName = "param"; String firstParamValue = "fpv"; String secondParamValue = "secondParamValue"; router.route("/first/:" + paramName + "/route").handler(rc -> { assertEquals(firstParamValue, rc.pathParam(paramName)); rc.reroute(HttpMethod.GET, "/second/" + secondParamValue + "/route"); }); router.route("/second/:" + paramName + "/route").handler(rc -> rc.response().setStatusMessage(rc.pathParam(paramName)).end()); testRequest(HttpMethod.GET, "/first/" + firstParamValue + "/route", 200, secondParamValue); }
@Test public void testMultipleHandlersMultipleConnectionsDelayedMixed() throws Exception { router.get("/:param").handler(routingContext -> { if (routingContext.pathParam("param").equals("fail")) { routingContext.fail(400); } else {
@Test public void testGetWithPlusPath2() throws Exception { router.get("/:param1").useNormalisedPath(false).handler(rc -> { assertEquals("/some+path", rc.normalisedPath()); assertEquals("some+path", rc.pathParam("param1")); assertEquals("some query", rc.request().getParam("q1")); rc.response().setStatusMessage("foo").end(); }); testRequest(HttpMethod.GET, "/some+path?q1=some+query", 200, "foo"); }
@Override public Object apply(final String name, final Class<?> paramType, final RoutingContext context) { // 1. Get path information return ZeroSerializer.getValue(paramType, context.pathParam(name)); } }
@Test public void testMultipleHandlersMixed() throws Exception { router.route().failureHandler(routingContext -> { routingContext.put("response", "fhandler1"); routingContext.next(); }); router.get("/:param").handler(routingContext -> { if (routingContext.pathParam("param").equals("fail")) routingContext.fail(500); routingContext.put("response", "handler1"); routingContext.next(); }).handler(routingContext -> { routingContext.put("response", routingContext.get("response") + "handler2"); routingContext.next(); }).handler(routingContext -> { HttpServerResponse response = routingContext.response(); response.setChunked(true); response.end(routingContext.get("response") + "handler3"); }).failureHandler(routingContext -> { routingContext.put("response", routingContext.get("response") + "fhandler2"); routingContext.next(); }).failureHandler(routingContext -> { HttpServerResponse response = routingContext.response(); response.setChunked(true); response.setStatusMessage("ERROR"); response.setStatusCode(500); response.end(routingContext.get("response") + "fhandler3"); }); testRequest(HttpMethod.GET, "/path", 200, "OK", "handler1handler2handler3"); testRequest(HttpMethod.GET, "/fail", 500, "ERROR", "fhandler1fhandler2fhandler3"); }
@Test public void testRegexGroupsNamesWithNestedGroups() throws Exception { router.getWithRegex("\\/(?<secondParam>[a-z]{2}(?<firstParam>[a-z]{2}))").handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam")) .end()); testRequest(HttpMethod.GET, "/bbaa", 200, "aa-bbaa"); }
@Test public void testRegexGroupsNames() throws Exception { router.getWithRegex("\\/(?<firstParam>[a-z]{2})(?<secondParam>[a-z]{2})").handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam")) .end()); testRequest(HttpMethod.GET, "/aabb", 200, "aa-bb"); }
@Test public void testPathParamsWithReroute() throws Exception { String paramName = "param"; String firstParamValue = "fpv"; String secondParamValue = "secondParamValue"; router.route("/first/:" + paramName + "/route").handler(rc -> { assertEquals(firstParamValue, rc.pathParam(paramName)); rc.reroute(HttpMethod.GET, "/second/" + secondParamValue + "/route"); }); router.route("/second/:" + paramName + "/route").handler(rc -> rc.response().setStatusMessage(rc.pathParam(paramName)).end()); testRequest(HttpMethod.GET, "/first/" + firstParamValue + "/route", 200, secondParamValue); }