Spring Boot☞ 统一异常处理
效果区:




代码区:
package com.wls.integrateplugs.exception.dto;
public class ErrorInfo<T> {
public static final Integer OK = 200;
public static final Integer ERROR = 500;
private Integer code;
private String message;
private String url;
private T data;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public static Integer getOK() {
return OK;
}
public static Integer getERROR() {
return ERROR;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
package com.wls.integrateplugs.exception; import com.wls.integrateplugs.mvc.enums.RespEnum; /**
* Created by wls on 2017/8/7.
*/
public class MyException extends RuntimeException {
private Integer code; public MyException() {
} public MyException(RespEnum respEnum) {
super(respEnum.getMsg());
this.code = respEnum.getCode();
} public Integer getCode() {
return code;
} public void setCode(Integer code) {
this.code = code;
}
}
package com.wls.integrateplugs.exception.controller; import com.wls.integrateplugs.exception.MyException;
import com.wls.integrateplugs.mvc.enums.RespEnum;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping(value = "/exception")
public class ExceptionController {
@RequestMapping("/html")
public String hello() throws Exception {
throw new Exception("发生错误");
} @RequestMapping("/json")
public String json() throws MyException {
throw new MyException(RespEnum.UNKNOW_CODE);
}
}
package com.wls.integrateplugs.exception.handle; import com.wls.integrateplugs.exception.MyException;
import com.wls.integrateplugs.exception.dto.ErrorInfo;
import com.wls.integrateplugs.mvc.util.http.RespUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; /**
* Created by wls on 2017/8/7.
*/
@ControllerAdvice
public class ExceptionHandle { Logger logger = LoggerFactory.getLogger(ExceptionHandle.class); /*@ExceptionHandler(value = Exception.class)
@ResponseBody
public RespUtil handle(Exception e){
// synchronized ()
if(e instanceof MyException){
MyException myException = (MyException) e;
return RespUtil.error(myException.getCode(),myException.getMessage());
}else{
logger.info("【异常信息】:{}",e);
return RespUtil.error(-1,"未知错误!");
}
}*/ @ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
ModelAndView mav = new ModelAndView();
mav.addObject("exception", e);
mav.addObject("url", req.getRequestURL());
mav.setViewName("error");
return mav;
} @ExceptionHandler(value = MyException.class)
@ResponseBody
public ErrorInfo<String> jsonErrorHandler(HttpServletRequest req, MyException e) throws Exception {
ErrorInfo<String> r = new ErrorInfo<>();
r.setMessage(e.getMessage());
r.setCode(ErrorInfo.ERROR);
r.setData("Some Data");
r.setUrl(req.getRequestURL().toString());
return r;
} }
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head lang="en">
<meta charset="UTF-8" />
<title>统一异常处理</title>
</head>
<body>
<h1>Error Handler</h1>
<div th:text="${url}"></div>
<div th:text="${exception.message}"></div>
</body>
</html>
Spring Boot☞ 统一异常处理的更多相关文章
- Spring Boot统一异常处理实践
摘要: SpringBoot异常处理. 原文:Spring MVC/Boot 统一异常处理最佳实践 作者:赵俊 前言 在 Web 开发中, 我们经常会需要处理各种异常, 这是一件棘手的事情, 对于很多 ...
- spring boot 统一异常处理
需求源自于任何一个业务的编写总会有各种各样的条件判断,需要时时手动抛出异常,又希望让接口返回友好的错误信息. spring boot提供的帮助是自动将异常重定向到路由为/error的控制器 但是我们又 ...
- Spring Boot统一异常处理方案示例
一.异常处理的原则 1.调用方法的时候返回布尔值来代替返回null,这样可以 NullPointerException.由于空指针是java异常里最恶心的异常. 2. catch块里别不写代码.空ca ...
- Spring MVC 统一异常处理
Spring MVC 统一异常处理 看到 Exception 这个单词都心慌 如果有一天你发现好久没有看到Exception这个单词了,那你会不会想念她?我是不会的.她如女孩一样的令人心动又心慌,又或 ...
- 编程小白入门分享三:Spring AOP统一异常处理
Spring AOP统一异常处理 简介 在Controller层,Service层,可能会有很多的try catch代码块.这将会严重影响代码的可读性."美观性".怎样才可以把更多 ...
- Spring Boot全局异常处理
本文为大家分享了Spring Boot全局异常处理,供大家参考,具体内容如下 1.后台处理异常 a.引入thymeleaf依赖 <!-- thymeleaf模板插件 --> <dep ...
- Spring Boot 统一返回结果及异常处理
在 Spring Boot 构建电商基础秒杀项目 (三) 通用的返回对象 & 异常处理 基础上优化.调整 一.通用类 1.1 通用的返回对象 public class CommonReturn ...
- spring boot 统一接口异常返回值
创建业务 Exception 一般在实际项目中,推荐创建自己的 Exception 类型,这样在后期会更容易处理,也比较方便统一,否则,可能每个人都抛出自己喜欢的异常类型,而造成代码混乱 Servic ...
- 使用Spring MVC统一异常处理实战
1 描述 在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理.每个过程都单独处理异常,系统的代码耦合 ...
随机推荐
- FU-A 分包
FU-A分包方式,以及从RTP包里面得到H.264数据和AAC数据的方法 [原创] RFC3984是H.264的baseline码流在RTP方式下传输的规范,这里只讨论FU-A分包方式,以及从RTP包 ...
- python复习之路-Day01
数据类型初识 1.数字 2 是一个整数的例子.长整数 不过是大一些的整数.3.23和52.3E-4是浮点数的例子.E标记表示10的幂.在这里,52.3E-4表示52.3 * 10-4.(-5+4j)和 ...
- ASP.NET MVC5入门指南
1.创建项目 文件 --> 新建 --> 项目 Visual C# --> Web --> ASP.NET Web应用程序 MVC此时处于选中状态,勾选“添加单元测试”(可选择 ...
- Java中UTC时间转换
import java.text.SimpleDateFormat; import java.util.Date; import java util.Calendar; public class Te ...
- 关于网页中行内元素的基线(baseline)、行高(line-height)、垂直对齐(vertical-align)等
CSS基线之道 http://www.qianduan.net/css-baseline-road.html 垂直对齐:vertical-align属性 http://www.ddcat.net/bl ...
- ie6绝对定位的块会被select元素遮挡的解决方案
RT(已无力吐槽ie),解决方法是:定义一个iframe,与想要显示的绝对定位的块设置为同一大小.放在同一个位置上.我的网页里绝对定位的元素是会随着鼠标移动显示和隐藏的,于是这个frame也要跟着显示 ...
- numpy的通用函数
通用函数:快速的元素级数组函数 通用函数是一种对ndarry中的数据执行元素级运算的函数,可以看作是简单函数(接受一个或多个标量值,并产生一个或多个标量值)的矢量化包装器. 一元func: abs丶f ...
- 「小程序JAVA实战」小程序 loading 提示框与页面跳转(37)
转自:https://idig8.com/2018/09/02/xiaochengxujavashizhanxiaochengxu-loading-tishikuangyuyemiantiaozhua ...
- bootstrap中的modal 模态弹出框不能放在 form_for里面,一弹出modal会自动submit掉form
bootstrap中的modal 模态弹出框不能放在 form_for里面,一弹出modal会自动submit掉form
- ubuntu 18.04 - server版 开机启动脚本
ubuntu 18.04 不再使用 inited 管理系统,改用 systemd systemd 默认读取 /etc/systemd/system 下的文件,该目录下的文件会链接/lib/system ...