spring boot学习(八)---统一异常处理
来源:互联网 发布:数据库的基本特点是 编辑:程序博客网 时间:2024/06/05 18:29
八 统一异常处理
1.内容
统一对异常进行高效的处理
2.添加统一返回报文格式
在entity包下新建Result类
package com.boot.entity;/** * http请求返回的最外层对象 * @author LCJA * @param <T> * */public class Result<T> { private Integer code; private String msg; private T data; public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; }}
修改StuController下的getAdd方法
@RequestMapping(method=RequestMethod.POST) public Result<Stu> getAdd(@Valid Stu stu,BindingResult bindresult){ if(bindresult.hasErrors()){ Result result = new Result(); result.setCode(1); result.setMsg(bindresult.getFieldError().getDefaultMessage()); return result; } stu.setAge(stu.getAge()); stu.setName(stu.getName()); stu = stuRepository.save(stu); Result result = new Result(); result.setCode(0); result.setMsg("成功"); result.setData(stu); return result; }
重新调用该方法后
如果不通过校验
3.添加统一返回方法
在utils下写ResultUtil类
package com.boot.utils;import com.boot.entity.Result;public class ResultUtil { public static Result success(Object object){ Result result = new Result(); result.setCode(0); result.setMsg("成功"); result.setData(object); return result; } public static Result success(){ return success(null); } public static Result error(Integer code,String message){ Result result = new Result(); result.setCode(code); result.setMsg(message); return result; }}
修改StuController下的getAdd方法
@RequestMapping(method=RequestMethod.POST) public Result<Stu> getAdd(@Valid Stu stu,BindingResult bindresult){ if(bindresult.hasErrors()){ return ResultUtil.error(1, bindresult.getFieldError().getDefaultMessage()); } stu.setAge(stu.getAge()); stu.setName(stu.getName()); stu = stuRepository.save(stu); return ResultUtil.success(stu); }
数据库插入数据age=11 name=测试
新建handle包,在下新建ExceptionHandle类
package com.boot.handle;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseBody;import com.boot.entity.Result;import com.boot.utils.ResultUtil;@ControllerAdvicepublic class ExceptionHandle { @ExceptionHandler(value = Exception.class) @ResponseBody public Result handle(Exception e){ return ResultUtil.error(100, e.getMessage()); }}
4.自定义异常
创建自己的exception
创建exception包,包下建StuException
package com.boot.Exception;public class StuException extends RuntimeException{ private Integer code; public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public StuException(Integer code,String message) { super(message); this.code = code; }}
在service实现类中getAge中修改
public void getAge(Integer id) throws Exception{ Stu stu = sturepository.findOne(id); Integer age = Integer.valueOf(stu.getAge()); if(age < 10 ){ throw new StuException(100,"小于10岁"); }else if( age > 10 && age < 16 ){ throw new StuException(101,"小于16岁"); } }
修改ExceptionHandle类
package com.boot.handle;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseBody;import com.boot.Exception.StuException;import com.boot.entity.Result;import com.boot.utils.ResultUtil;@ControllerAdvicepublic class ExceptionHandle { @ExceptionHandler(value = Exception.class) @ResponseBody public Result handle(Exception e){ if(e instanceof StuException){ StuException stuexception= (StuException) e; return ResultUtil.error(stuexception.getCode(),stuexception.getMessage()); } return ResultUtil.error(-1, "未知错误"); }}
5.用枚举表示异常
package com.boot.enums;public enum ResultEnum { UNKONW_ERROR(-1,"未知错误"), SUCCESS(0,"成功"), TEN(100,"小于10岁"), SIXTEEN(101,"小于16岁") ; private Integer code; private String message; private ResultEnum(Integer code, String message) { this.code = code; this.message = message; } 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; }}
在StuException中添加构造函数
public StuException(ResultEnum result) { super(result.getMessage()); this.code = result.getCode(); }
这样StuServiceImpl中的getAge可以这样写
public void getAge(Integer id) throws Exception{ Stu stu = sturepository.findOne(id); Integer age = Integer.valueOf(stu.getAge()); if(age < 10 ){ throw new StuException(ResultEnum.TEN); }else if( age > 10 && age < 16 ){ throw new StuException(ResultEnum.SIXTEEN); } }
阅读全文
0 0
- spring boot学习(八)---统一异常处理
- Spring-Boot 统一异常处理
- Spring Boot 异常统一处理
- Spring boot统一异常处理
- Spring Boot 系列(八)@ControllerAdvice 拦截异常并统一处理
- Spring Boot(四)通用CRUD案例、异常统一处理
- spring boot基础之统一异常处理(小demo)
- spring boot 教程(四) 统一异常处理
- spring boot统一处理结果(含异常)
- spring boot之统一错误异常处理
- Spring Boot 统一异常处理的案例
- spring boot AOP和spring boot统一异常处理
- Spring Boot学习(六)之Web应用的统一异常处理
- Spring Boot中Web应用的统一异常处理
- Spring Boot中Web应用的统一异常处理
- Spring Boot中Web应用的统一异常处理
- Spring Boot中Web应用的统一异常处理
- Spring Boot中Web应用的统一异常处理
- 【python】turtle库
- 通过监听文件日志统计连接情况的perl脚本
- ConfigReader(五十四)—— ReadSkillManager
- 改变select下拉框中对应option的样式颜色已经展示的option如何进行颜色对应
- Linux常用命令2
- spring boot学习(八)---统一异常处理
- python基础(一)
- 官方下载的java版本ckfinder2.6如何去除“帮助”提示
- mybatis延迟加载
- Unity Shader 剔除黑色背景
- 量化投资:第3节 滑点策略与交易手续费
- 写一个函数,输入一行字符,将此字符串中最长的单词输出。
- 用树莓派做业务服务器(RaspberryPi + raspbian 跑 mediawiki 与 trac)[draft]
- git如何合并两个仓库