相关疑难解决方法(0)

sparkjava:路由必须是main方法吗?

我是sparkjava的新手,总体上喜欢它.但是,是否必须在main方法中定义新的路由/端点?对于任何重要的Web应用程序,这将导致一个非常长的主方法,或者我需要有多个主要方法(因此在多个实例之间拆分服务器资源).

这两个sparkjava文档页面似乎在main方法中定义了路径:http://sparkjava.com/documentation.html#routes 和http://sparkjava.com/documentation.html#getting-started.

有没有其他方法可以做到这一点,我没有看到?粗略的谷歌搜索没有向我展示更好的方式......

=========

以下是我根据安德鲁的答案做的完整解决方案.在我看来,在main方法之外添加端点应该是sparkjava文档页面的一部分:

主要方法:

public static void main(String[] args) {
    //Do I need to do something more with the Resource instance so that sparkjava notices it and/or reads the routes?
    Resource resource= new Resource(new Service());
}
Run Code Online (Sandbox Code Playgroud)

我的资源:

import static spark.Spark.*;
class Resource{

    private Service service;

    Resource(Service service){
        this.service = service;
        setupEndpoints();
    }

    private void setupEndpoints() {

        get("/user/:id", "application/json",(request, response)
                -> service.find(request.params(":id")), new JsonTransformer());

        get("/users", "application/json", (request, response)
                -> service.findAll(), new JsonTransformer());
    }
} …
Run Code Online (Sandbox Code Playgroud)

routes spark-java

5
推荐指数
2
解决办法
4598
查看次数

标签 统计

routes ×1

spark-java ×1