Spring Boot学习(六)之Web应用的统一异常处理
2017-11-18 14:34
1041 查看
我们在做web项目中,都遇到了404,405,500这样的错误信息,需要需要处理异常信息,
启动该应用,访问一个不存在的URL,或是修改处理内容,直接抛出异常,如:
SpringBoot中默认带了error的映射,但是这个错误页面显示给用户并不是很友好。
统一异常处理
虽然,SpringBoot中实现了默认的error映射,但是在实际应用中,上面你的错误页面对用户来说并不够友好,我们通常需要去实现我们自己的异常提示。
下面我们开始学习统一处理:
创建全局异常处理类:通过使用@ControllerAdvice定义统一的异常处理类,而不是在每个Controller中逐个定义。@ExceptionHandler用来定义函数针对的异常类型,最后将Exception对象和请求URL映射到error.html中
实现error.html页面展示:在templates目录下创建error.html,将请求的URL和Exception对象的message输出。
启动该应用,访问:http://localhost:8080/hello,可以看到如下错误提示页面。
通过实现上述内容之后,我们只需要在Controller中抛出Exception,当然我们可能会有多种不同的Exception。然后在@ControllerAdvice类中,根据抛出的具体Exception类型匹配@ExceptionHandler中配置的异常类型来匹配错误映射和处理。
返回JSON格式
在上述例子中,通过@ControllerAdvice统一定义不同Exception映射到不同错误处理页面。而当我们要实现RESTfulAPI时,返回的错误是JSON格式的数据,而不是HTML页面,这时候我们也能轻松支持。
本质上,只需在@ExceptionHandler之后加入@ResponseBody,就能让处理函数return的内容转换为JSON格式。
下面以一个具体示例来实现返回JSON格式的异常处理。
创建统一的JSON返回对象,code:消息类型,message:消息内容,url:请求的url,data:请求返回的数据
创建一个自定义异常,用来实验捕获该异常,并返回json
Controller中增加json映射,抛出MyException异常
为MyException异常创建对应的处理
启动应用,访问:http://localhost:8080/json,可返回如下内容:
到此我们完成了统一异常处理,欢迎学习交流,
欢迎大家一起交流学习SpringBoot,java等领域的技术,交流群:587674051博客的源码也在里面
启动该应用,访问一个不存在的URL,或是修改处理内容,直接抛出异常,如:
1 | @RequestMapping ( "/hello" ) |
2 | public String hello() throws Exception { |
3 | throw new Exception( "发生错误" ); |
4 | } |
统一异常处理
虽然,SpringBoot中实现了默认的error映射,但是在实际应用中,上面你的错误页面对用户来说并不够友好,我们通常需要去实现我们自己的异常提示。
下面我们开始学习统一处理:
创建全局异常处理类:通过使用@ControllerAdvice定义统一的异常处理类,而不是在每个Controller中逐个定义。@ExceptionHandler用来定义函数针对的异常类型,最后将Exception对象和请求URL映射到error.html中
01 | import com.didispace.dto.ErrorInfo; |
02 | import org.springframework.web.bind.annotation.ControllerAdvice; |
03 | import org.springframework.web.bind.annotation.ExceptionHandler; |
04 | import org.springframework.web.bind.annotation.ResponseBody; |
05 | import org.springframework.web.servlet.ModelAndView; |
06 |
07 | import javax.servlet.http.HttpServletRequest; |
08 | /** |
09 | * 筱进GG |
10 | */ |
11 | @ControllerAdvice |
12 | public class GlobalExceptionHandler { |
13 |
14 | @ExceptionHandler (value class ) |
15 | public ModelAndView throws Exception { |
16 | ModelAndView new ModelAndView(); |
17 | mav.addObject( "exception" , e); |
18 | mav.addObject( "url" , req.getRequestURL()); |
19 | mav.setViewName( "error" ); |
20 | return mav; |
21 | } |
22 | } |
01 | <!DOCTYPE html> |
02 | < html > |
03 | < head lang = "en" > |
04 | < meta charset = "UTF-8" /> |
05 | < title >统一异常处理</ title > |
06 | </ head > |
07 | < body > |
08 | < h1 >Error Handler</ h1 > |
09 | < div th:text = "${url}" ></ div > |
10 | < div th:text = "${exception.message}" ></ div > |
11 | </ body > |
12 | </ html > |
通过实现上述内容之后,我们只需要在Controller中抛出Exception,当然我们可能会有多种不同的Exception。然后在@ControllerAdvice类中,根据抛出的具体Exception类型匹配@ExceptionHandler中配置的异常类型来匹配错误映射和处理。
返回JSON格式
在上述例子中,通过@ControllerAdvice统一定义不同Exception映射到不同错误处理页面。而当我们要实现RESTfulAPI时,返回的错误是JSON格式的数据,而不是HTML页面,这时候我们也能轻松支持。
本质上,只需在@ExceptionHandler之后加入@ResponseBody,就能让处理函数return的内容转换为JSON格式。
下面以一个具体示例来实现返回JSON格式的异常处理。
创建统一的JSON返回对象,code:消息类型,message:消息内容,url:请求的url,data:请求返回的数据
01 | /** |
02 | * 筱进GG |
03 | */ |
04 | public class ErrorInfo<T> { |
05 |
06 | public static final Integer 0 ; |
07 | public static final Integer 100 ; |
08 |
09 | private Integer code; |
10 | private String message; |
11 | private String url; |
12 | private T data; |
13 |
14 | public String |
15 | return url; |
16 | } |
17 |
18 | public void setUrl(String |
19 | this .url |
20 | } |
21 |
22 | public static Integer |
23 | return OK; |
24 | } |
25 |
26 | public static Integer |
27 | return ERROR; |
28 | } |
29 |
30 | public Integer |
31 | return code; |
32 | } |
33 |
34 | public void setCode(Integer |
35 | this .code |
36 | } |
37 |
38 | public String |
39 | return message; |
40 | } |
41 |
42 | public void setMessage(String |
43 | this .message |
44 | } |
45 |
46 | public T |
47 | return data; |
48 | } |
49 |
50 | public void setData(T |
51 | this .data |
52 | } |
53 |
54 | } |
01 | /** |
02 | * 筱进GG |
03 | */ |
04 | public class MyException extends Exception { |
05 |
06 | public MyException(String |
07 | super (message); |
08 | } |
09 |
10 | } |
1 | @RequestMapping ( "/json" ) |
2 | public String throws MyException { |
3 | throw new MyException( "发生错误2" ); |
4 | } |
01 | @ExceptionHandler (value class ) |
02 | @ResponseBody |
03 | public ErrorInfo<String> throws Exception { |
04 | ErrorInfo<String> new ErrorInfo<>(); |
05 | r.setMessage(e.getMessage()); |
06 | r.setCode(ErrorInfo.ERROR); |
07 | r.setData( "Some Data" ); |
08 | r.setUrl(req.getRequestURL().toString()); |
09 | return r; |
10 | } |
1 | { |
2 | code: 100, |
3 | data: "Some Data" , |
4 | message: "发生错误2" , |
5 | url: " |
6 | } |
欢迎大家一起交流学习SpringBoot,java等领域的技术,交流群:
相关文章推荐
- Spring Boot中Web应用的统一异常处理
- Spring Boot 与 Kotlin Web应用的统一异常处理
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十一)Web应用的统一异常处理
- 企业分布式微服务云SpringCloud SpringBoot mybatis (五)Spring Boot中Web应用的统一异常处理
- Spring Boot中Web应用的统一异常处理
- 企业分布式微服务云SpringCloud SpringBoot mybatis (五)Spring Boot中Web应用的统一异常处理
- Spring Boot中Web应用的统一异常处理
- Spring Boot中Web应用的统一异常处理
- Spring Boot中Web应用的统一异常处理
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十一)Web应用的统一异常处理
- Spring Boot中Web应用的统一异常处理
- Spring Boot中Web应用的统一异常处理
- SpringCloud SpringBoot mybatis 分布式微服务(六)Spring Boot中Web应用的统一异常处理
- Spring Boot中Web应用的统一异常处理
- spring-boot实战【07】【转】:Spring Boot中Web应用的统一异常处理
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十一)Web应用的统一异常处理
- Spring Boot中Web应用的统一异常处理
- Spring Boot中Web应用的统一异常处理
- Spring Boot中Web应用的统一异常处理
- Spring Boot中Web应用的统一异常处理