How can you configure context path using com.sparkjava:spark-core
with jetty?
EDIT:
Instead of root app context being http://mydomain.xyz
the app would be accessed through http://mydomain.xyz/custom-context
.
How can you configure context path using com.sparkjava:spark-core
with jetty?
EDIT:
Instead of root app context being http://mydomain.xyz
the app would be accessed through http://mydomain.xyz/custom-context
.
spark-java does not directly support setting the context as a separate setting, but it still can be done. Probably the easiest way is to use the Path groups as outlined in the docs.
Example from the documentation:
path("/api", () -> {
before("/*", (q, a) -> log.info("Received api call"));
path("/email", () -> {
post("/add", EmailApi.addEmail);
put("/change", EmailApi.changeEmail);
delete("/remove", EmailApi.deleteEmail);
});
path("/username", () -> {
post("/add", UserApi.addUsername);
put("/change", UserApi.changeUsername);
delete("/remove", UserApi.deleteUsername);
});
});
So, by putting all route definitions within a path
-call, you can easily set a context-like base-route for all your URLs. Theoretically, you could load this base-path (or context) from a configuration file, then it would be:
String context = myConfig.getContext();
path(context, () -> {
get("/", getHome());
...
});