2017-05-30 60 views
1

我正尝试在服务器端使用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。

+0

你确定你得到的错误是404而不是500吗? – SHG

+0

是的,这是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

+0

你如何对'/ login'执行获取请求? – SHG

回答

2

的问题是在函数renderIndex()。在使用正确的资源路径(即/public/index.html)之后,变量url不再为空,但根据您在评论中所说的内容,这有些奇怪(jar:file:/home/gorrtack/workspace/Farot/target/Farot-1.0-SNA‌PSHOT-jar-with-depen‌​dencies.jar!/public/‌​index.html),无效的路径

Paths.get()试图解决此路径失败并抛出NoSuchFileException(这是一个IOException)。然后,你赶上它在catch区块,而返回空。返回null是错误的,这是你得到错误404的原因。

因此,你需要:

  1. 要改变的东西在你的项目的结构等等的index.html的路径是正确的。那么你会避免在这种情况下的问题。
  2. 正确处理异常,意味着 - 不返回null。在这些情况下决定你想要做什么,然后,如果你愿意,你仍然可以向客户端发送正常的错误消息和/或使用API​​或任何any other response APIs来自己设置响应状态。
+0

谢谢,但我从方法'App.class.getResource()'得到这个路径应该是正确的。我使用maven来构建我的项目,并且所有的文件结构都像他们推荐的那样:** root ** - > ** src ** - > ** main ** - > ** java **与.java文件的分离和**资源**,其中我的应用程序的前端部分保留所有静态文件。 – user1856728

+0

似乎结构很好。但是,在我的项目中(也是maven,相同的结构),我从'App.class.getResource()'得到正确的路径。也许检查pom.xml配置? – SHG

+0

里面的**资源**文件夹我有** public **。如果我将hello.txt放在公共文件夹中,则可以在localhost:4567/hello.txt中找到,因为我的代码中有staticFiles.location(“/ public”);'。 POM配置是https://github.com/Koroffin/farot/blob/master/pom.xml – user1856728

1

在renderIndex()方法,访问路径如下:

URL url = App.class.getResource("/public/index.html");

+0

这是真的,但它没有解释404错误,应该是500,如果是这样的话 – SHG

+0

同意...试了他的代码,我得到500内部错误... – utkarsh31

+0

它仍然是404错误。输出:[qtp199089828-23] INFO spark.http.matching.MatcherFilter - 请求的路由[/ l ogin]尚未在Spark for Accept中映射:[text/html,application/xhtml + xml,application/xml; q = 0.9,image/webp,*/*; q = 0.8] – user1856728