spark.http.matching.MatcherFilter-请求的路由[/ test]尚未在Spark中被映射为接受:[* / *]

时间:2019-11-20 20:27:28

标签: spark-java

我有一个Spark API,我正在尝试发出POST请求。我不断在控制台中收到404响应状态和此错误:

  

spark.http.matching.MatcherFilter-请求的路由[/ test]尚未在Spark中映射为接受:[* / *]

我已经验证了我的API适用于GET请求,并且已经读过类似的线程,但是似乎仍然存在问题。这是我的班级样子:

    options("/*",
            (request, response) -> {

                String accessControlRequestHeaders = request
                        .headers("Access-Control-Request-Headers");
                if (accessControlRequestHeaders != null) {
                    response.header("Access-Control-Allow-Headers",
                            accessControlRequestHeaders);
                }

                String accessControlRequestMethod = request
                        .headers("Access-Control-Request-Method");
                if (accessControlRequestMethod != null) {
                    response.header("Access-Control-Allow-Methods",
                            accessControlRequestMethod);
                }

                return "OK";
            });

    before("/*", (request, response) -> response.header("Access-Control-Allow-Origin", "*"));


    post("/test", (request, response) -> {
    response.type("application/json");
    response.status(200);

    // Here to not return a null value
    return new Gson()
      .toJson(new Color(1));
    });

我正在测试邮递员的请求。在那里,我有标题 'Accept': 'application/json'和包含'body': 'body'的主体以进行测试。

我知道我的POST方法实际上并没有做任何保证它是POST方法的事情,但这只是概念的证明。

1 个答案:

答案 0 :(得分:1)

似乎您正在定义路线/test/,但是在测试时,您正在导航至/test。这些不是Sparkjava中的相等路由。

如果您想使/test/test/具有相同的实现-我将/test定义为路由,然后使用{ {3}}。像这样:

/test/

(实际上,您实际上希望使此代码更通用)。