SparkJava自定义错误页面

时间:2013-11-01 16:47:34

标签: spark-java

使用 Spark 微型网页框架时,是否有人知道如何覆盖现有404错误页面?

默认错误页面为:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 404 </title>
</head>
<body>
<h2>HTTP ERROR: 404</h2>
<p>Problem accessing /strangepage. Reason:
<pre>    Not Found</pre></p>
<hr /><i><small>Powered by Jetty://</small></i>
</body>
</html>

我想修改此自定义错误页面(或者可能将其重定向到另一条路线):

get(new Route("/404") {
   @Override
   public Object handle(Request request, Response response) {
       response.type("text/html");
       return "Error page 404";
   }
});

7 个答案:

答案 0 :(得分:4)

尝试使用下面的提示

在最后一条路线后立即添加这些行,因为Spark关心订单

Spark.get("*", (req, res) -> {    
    if(!req.pathInfo().startsWith("/static")){
        res.status(404);
        return TEMPLATE_ENGINE.render(ModelAndView modelAndView);
    }
    return null;
});

所有请求(包括静态请求)都不匹配将在此处捕获的所有上层路由。因此,您必须将奇怪的请求和静态请求与IF语句分开。你应该在这里将你的错误html页面作为字符串返回。

所有静态请求都由另一个处理程序处理,您必须返回NULL以强制Spark调用另一个处理程序作为正常情况。

答案 1 :(得分:2)

如果您在网络服务器上部署应用,则可以将错误映射到火花路线。

<filter>
    <filter-name>SparkFilter</filter-name>
    <filter-class>spark.servlet.SparkFilter</filter-class>
    <init-param>
        <param-name>applicationClass</param-name>
        <param-value>com.company.YourApplication</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>SparkFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

<error-page>
    <error-code>404</error-code>
    <location>/404</location> <!-- this is your route-->
</error-page>

答案 2 :(得分:1)

我认为您可以使用Spark过滤器。过滤掉不允许的路线。您可以在过滤器中渲染新模板。

这是文档中的一个示例。

before(new Filter() { // matches all routes
    @Override
    public void handle(Request request, Response response) {
        boolean authenticated;
        // ... check if authenticated
        if (!authenticated) {
            halt(401, "You are not welcome here");
        }
    }
 });

答案 3 :(得分:1)

在Spark 2.3文档中:

get("/throwexception", (request, response) -> {
    throw new NotFoundException();
});

exception(NotFoundException.class, (e, request, response) -> {
    response.status(404);
    response.body("Resource not found");
});

我发现代替&#34; / throwexception&#34;,&#34; / *&#34;得到所有未找到的页面。如果你的网址结构比我正在做的更复杂,这可能不起作用。我无法在我的项目中解决NotFoundException,所以我认为你可以自己制作异常或抛出一个内置的异常。

答案 4 :(得分:1)

显然这是Spark一段时间没能解决的问题。但我能够想出一个解决方法: https://github.com/perwendel/spark/issues/197#issuecomment-213952246

基本上是自己启动和配置嵌入式Jetty,而不是让Spark为你做这件事。

答案 5 :(得分:1)

对于那些想要处理所有异常并显示错误消息的人,这是一种声明处理程序的简单方法:

exception(Exception.class, exceptionHandler());

然后是一个简单的实现:

private ExceptionHandler exceptionHandler() {
    return (e, req, res) -> {
        res.status(500);
        res.body("<h1>Exception occurred</h1><div>" + e.getMessage() + "</div>");
    };
}

答案 6 :(得分:1)

这是一个老问题,但因为它仍然有问题。

现在您可以按如下方式传递404:

notFound("<html><body><h1>Custom 404 handling</h1></body></html>");