我正尝试在服务器端使用Spark创建SPA。 这是我App.java:在Path.Web.api.Account.DEFAULTJava,Spark,return index.html
package com.farot;
import java.util.HashMap;
import java.util.UUID;
import java.net.URL;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.charset.Charset;
import java.io.IOException;
import com.google.gson.Gson;
import static spark.Spark.*;
import com.farot.utils.Path;
import com.farot.controllers.UserController;
import com.farot.controllers.AccountController;
import com.farot.controllers.MapController;
public class App
{
private static Gson gson = new Gson();
private static String renderIndex() {
try {
URL url = App.class.getResource("index.html");
return new String(Files.readAllBytes(Paths.get(url.toURI())), Charset.defaultCharset());
} catch (IOException | URISyntaxException e) {
System.out.println(e.getMessage());
}
return null;
}
public static void main(String[] args)
{
staticFiles.location("/public");
before((req, res) -> {
String path = req.pathInfo();
if (path.endsWith("/"))
res.redirect(path.substring(0, path.length() - 1));
});
// Site pages
get("/", "text/html", (req, res) -> renderIndex());
get("/login", "text/html", (req, res) -> renderIndex());
post(Path.Web.api.Account.DEFAULT, (req, res) -> {
return AccountController.create(req, res);
}, gson::toJson);
}
}
POST请求按预期工作,但在/登录请求返回404什么可能是错误的? index.html的路径是/resources/public/index.html。
你确定你得到的错误是404而不是500吗? – SHG
是的,这是404错误。这里是控制台输出:[qtp199089828-23] INFO spark.http.matching.MatcherFilter - 请求的路由[/ login]尚未在Spark for Accept中映射:[text/html,application/xhtml + xml,application/xml; q = 0.9,image /webp,/;q=0.8] – user1856728
你如何对'/ login'执行获取请求? – SHG