简体   繁体   English

如何使用Spark-Java配置上下文路径

[英]How to configure context path using Spark-Java

How can you configure context path using com.sparkjava:spark-core with jetty? 如何使用com.sparkjava:spark-core和jetty配置上下文路径?

EDIT: 编辑:

Instead of root app context being http://mydomain.xyz the app would be accessed through http://mydomain.xyz/custom-context . 相反,根应用上下文是http://mydomain.xyz应用程序将通过访问http://mydomain.xyz/custom-context

see this post on the same subject but in spring-boot 看到这篇文章在同一主题,但在春季启动

spark-java does not directly support setting the context as a separate setting, but it still can be done. spark-java不直接支持将上下文设置为单独的设置,但是仍然可以完成。 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. 因此,通过将所有路由定义放在path调用中,您可以轻松地为所有URL设置类似上下文的基本路由。 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());
    ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM