0
votes

I am trying to convert Http service written using Vertx to Akka Http (2.0.5) in Java. I had a couple of URLs like -

  • "/api/addresses/id/:id/limit/:limit"
  • "/api/addresses/:id/:shortid/:type"

How to get the above paths matched?

Also, how can the static resources (like html, js, css files) be served from a resource directory? Is this path matcher fine - pathPrefix("assets/*.html", () -> getFromResourceDirectory("assets"))

1

1 Answers

0
votes

I was able to get the first part working, but was unable to figure out how to serve static resources. Here's the working code that did the trick for me -

pathPrefix("api", () -> path(segment("addresses").slash("id").slash(segment()).slash("limit").slash(segment()), (String id, String limit) -> {
                return getEitherListResponse(
                        () -> doSomethingWith(id, limit));
            })),

pathPrefix("api", () -> path(segment("addresses").slash(segments()), (List<String> list) -> {
                if (list.size() == 2) {
                    String id = list.get(0);
                    String shortId = list.get(1);

                    return getEitherListResponse(
                            () -> doSomethingWith(id, shortId));
                } else {
                    String id = list.get(0);
                    String shortId = list.get(1);
                    String type = list.get(2);

                    return getEitherListResponse(
                            () -> doSomethingWith(id, shortId, type));
                }
            }))