接着前面几篇web处理请求的博文,本文将说明,当出现异常的场景下,如404请求url不存在,,403无权,500服务器异常时,咱们能够如何处理html
原文友链: SpringBoot系列教程web篇之40四、500异常页面配置java
首先得搭建一个web应用才有可能继续后续的测试,借助SpringBoot搭建一个web应用属于比较简单的活;git
建立一个maven项目,pom文件以下github
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7</version>
<relativePath/> <!-- lookup parent from update -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.45</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
复制代码
依然是通常的流程,pom依赖搞定以后,写一个程序入口web
/** * Created by @author yihui in 15:26 19/9/13. */
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
复制代码
在SpringBoot项目中,自己提供了一个默认的异常处理页面,当咱们但愿使用自定义的404,500等页面时,能够如何处理呢?spring
在默认的状况下,要配置异常页面很是简单,在资源路径下面,新建 error
目录,在下面添加400.html
, 500html
页面便可json
项目结构如上,注意这里的实例demo是没有使用模板引擎的,因此咱们的异常页面放在static目录下;若是使用了如FreeMaker模板引擎时,能够将错误模板页面放在template目录下服务器
接下来实际测试下是否生效, 咱们先定义一个可能出现服务器500的服务websocket
@Controller
@RequestMapping(path = "page")
public class ErrorPageRest {
@ResponseBody
@GetMapping(path = "divide")
public int divide(int sub) {
System.out.println("divide1");
return 1000 / sub;
}
}
复制代码
请求一个不存在的url,返回咱们定义的400.html
页面app
<html>
<head>
<title>404页面</title>
</head>
<body>
<h3>页面不存在</h3>
</body>
</html>
复制代码
请求一个服务器500异常,返回咱们定义的500.html
页面
<html>
<head>
<title>500页面</title>
</head>
<body>
<h2 style="color: red;">服务器出现异常!!!</h2>
</body>
</html>
复制代码
看上面的使用比较简单,天然会有个疑问,这个异常页面是怎么返回的呢?
从项目启动的日志中,注意一下RequestMappingHandlerMapping
能够发现里面有个/error
的路径不是咱们本身定义的,从命名上来看,这个多半就是专门用来处理异常的Controller -> BasicErrorController
, 部分代码以下
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {
@Override
public String getErrorPath() {
return this.errorProperties.getPath();
}
@RequestMapping(produces = "text/html")
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
HttpStatus status = getStatus(request);
Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(
request, isIncludeStackTrace(request, MediaType.TEXT_HTML)));
response.setStatus(status.value());
ModelAndView modelAndView = resolveErrorView(request, response, status, model);
return (modelAndView != null) ? modelAndView : new ModelAndView("error", model);
}
@RequestMapping
@ResponseBody
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.ALL));
HttpStatus status = getStatus(request);
return new ResponseEntity<>(body, status);
}
}
复制代码
这个Controller中,一个返回网页的接口,一个返回Json串的接口;咱们前面使用的应该是第一个,那咱们什么场景下会使用到第二个呢?
Accept
,来限定咱们只但愿获取json的返回便可本篇内容比较简单,概括为两句话以下
/error
目录下尽信书则不如,以上内容,纯属一家之言,因我的能力有限,不免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激
下面一灰灰的我的博客,记录全部学习和工做中的博文,欢迎你们前去逛逛