首页 > 解决方案 > Spark java返回html - 500内部服务器错误

问题描述

我想在我的 spark java 应用程序中关联 URL 和 index.html。如果我添加这条路线,它可以正常工作:

get("/api/hello", (req, res) -> "hello");

我打开 URL:http://localhost:8080/api/hello 并看到“你好”

如果我添加这条路线:

get("/api/index", (req, res) -> new ThymeleafTemplateEngine().render(
                new ModelAndView(new HashMap<>(), "/index.html")
        ));

webapp/index.html 中的这个 index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<H1>index</H1>
</body>
</html>

这个 web.xml

<web-app
        xmlns="http://java.sun.com/xml/ns/javaee"
        version="2.5">
    <filter>
        <filter-name>SparkFilter</filter-name>
        <filter-class>spark.servlet.SparkFilter</filter-class>
        <init-param>
            <param-name>applicationClass</param-name>
            <param-value>com.pavel.Main</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>SparkFilter</filter-name>
        <url-pattern>/api/*</url-pattern>
    </filter-mapping>
</web-app>

并打开http://localhost:8080/api/index

我看到 500 内部服务器错误

标签: javaspark-java

解决方案


推荐阅读