SparkJava自定义错误页面

dns*_*dns 5 spark-java

有没有人知道如何在使用Spark微型Web框架时覆盖现有的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>
Run Code Online (Sandbox Code Playgroud)

我想编辑此自定义错误页面(或者可能将其重定向到另一个路径):

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

abh*_*dey 3

这是一个老问题,但仍然有答案。

现在您可以按如下方式处理 404:

notFound("<html><body><h1>Custom 404 handling</h1></body></html>");
Run Code Online (Sandbox Code Playgroud)