• SpringMVC+统一表现层返回值+异常处理器


    一、统一表现层返回值

      根据我们不同的处理方法,返回的数据格式都会不同,例如添加只返回true|false,删除同理,而查询却返回数据。

    Result类

    为此我们封装一个result类来用于表现层的返回。

    1. public class Result {
    2. //描述统一格式中的数据
    3. private Object data;
    4. //描述统一格式中的编码,用于区分操作,可以简化配置0或1表示成功失败
    5. private Integer code;
    6. //描述统一格式中的消息,可选属性
    7. private String msg;
    8. public Result() {
    9. }
    10. public Result(Integer code,Object data) {
    11. this.data = data;
    12. this.code = code;
    13. }
    14. public Result(Integer code, Object data, String msg) {
    15. this.data = data;
    16. this.code = code;
    17. this.msg = msg;
    18. }
    19. public Object getData() {
    20. return data;
    21. }
    22. public void setData(Object data) {
    23. this.data = data;
    24. }
    25. public Integer getCode() {
    26. return code;
    27. }
    28. public void setCode(Integer code) {
    29. this.code = code;
    30. }
    31. public String getMsg() {
    32. return msg;
    33. }
    34. public void setMsg(String msg) {
    35. this.msg = msg;
    36. }
    37. }

    Code类

    Code类不是固定的,是商量好的一种编号,每种编号对应业务操作的不同状态。

    1. public class Code {
    2. //Code类的常量设计也不是固定的,可以根据需要自行增减。
    3. public static final Integer SAVE_OK = 20011;
    4. public static final Integer DELETE_OK = 20021;
    5. public static final Integer UPDATE_OK = 20031;
    6. public static final Integer GET_OK = 20041;
    7. public static final Integer SAVE_ERR = 20010;
    8. public static final Integer DELETE_ERR = 20020;
    9. public static final Integer UPDATE_ERR = 20030;
    10. public static final Integer GET_ERR = 20040;
    11. }

     BookController类

    1. //统一每一个控制器方法返回值
    2. @RestController
    3. @RequestMapping("/books")
    4. public class BookController {
    5. @Autowired
    6. private BookService bookService;
    7. @PostMapping
    8. public Result save(@RequestBody Book book) {
    9. boolean flag = bookService.save(book);
    10. return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    11. }
    12. @PutMapping
    13. public Result update(@RequestBody Book book) {
    14. boolean flag = bookService.update(book);
    15. return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    16. }
    17. @DeleteMapping("/{id}")
    18. public Result delete(@PathVariable Integer id) {
    19. boolean flag = bookService.delete(id);
    20. return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    21. }
    22. @GetMapping("/{id}")
    23. public Result getById(@PathVariable Integer id) {
    24. Book book = bookService.getById(id);
    25. Integer code = book != null ? Code.GET_OK : Code.GET_ERR;
    26. String msg = book != null ? "" : "数据查询失败,请重试!";
    27. return new Result(code,book,msg);
    28. }
    29. @GetMapping
    30. public Result getAll() {
    31. List bookList = bookService.getAll();
    32. Integer code = bookList != null ? Code.GET_OK : Code.GET_ERR;
    33. String msg = bookList != null ? "" : "数据查询失败,请重试!";
    34. return new Result(code,bookList,msg);
    35. }
    36. }

    对应效果

    二、异常处理

      对于Dao层(数据层),Service层(服务层),Controller(业务控制层)等,所抛出的异常应该统一到业务控制层来处理。

    1.封装异常处理器类

    @RestControllerAdvice注解可以声明一个类为异常处理器类

    @ExceptionHandler注解是定义当前方法用于处理哪一种异常,通过形参可以把异常传进来

     

     

    2. 项目中的异常

      自定义异常都要继承RuntimeException这个类,这个类的父类是Exception,这个类是运行异常类,碰到了自动往业务层传递异常。

    BusinessException 自定义业务异常类

    1. //自定义异常处理器,用于封装异常信息,对异常进行分类
    2. public class BusinessException extends RuntimeException{
    3. private Integer code;
    4. public Integer getCode() {
    5. return code;
    6. }
    7. public void setCode(Integer code) {
    8. this.code = code;
    9. }
    10. //自动实现的构造方法有很多,只不过这两种实用
    11. public BusinessException(Integer code, String message) {
    12. super(message);
    13. this.code = code;
    14. }
    15. public BusinessException(Integer code, String message, Throwable cause) {
    16. super(message, cause);
    17. this.code = code;
    18. }
    19. }

     SystemException 自定义系统异常类

    1. //自定义异常处理器,用于封装异常信息,对异常进行分类
    2. public class SystemException extends RuntimeException{
    3. private Integer code;
    4. public Integer getCode() {
    5. return code;
    6. }
    7. public void setCode(Integer code) {
    8. this.code = code;
    9. }
    10. public SystemException(Integer code, String message) {
    11. super(message);
    12. this.code = code;
    13. }
    14. public SystemException(Integer code, String message, Throwable cause) {
    15. super(message, cause);
    16. this.code = code;
    17. }
    18. }

    Code类的补充

    1. public static final Integer SYSTEM_ERR = 50001;
    2. public static final Integer SYSTEM_TIMEOUT_ERR = 50002;
    3. public static final Integer SYSTEM_UNKNOW_ERR = 59999;
    4. public static final Integer BUSINESS_ERR = 60002;

     异常控制类-ProjectExceptionAdvice

    拦截并处理异常

    1. //@RestControllerAdvice用于标识当前类为REST风格对应的异常处理器
    2. @RestControllerAdvice
    3. public class ProjectExceptionAdvice {
    4. //@ExceptionHandler用于设置当前处理器类对应的异常类型
    5. @ExceptionHandler(SystemException.class)
    6. public Result doSystemException(SystemException ex){
    7. //记录日志
    8. //发送消息给运维
    9. //发送邮件给开发人员,ex对象发送给开发人员
    10. return new Result(ex.getCode(),null,ex.getMessage()); //规范返回结果
    11. }
    12. @ExceptionHandler(BusinessException.class)
    13. public Result doBusinessException(BusinessException ex){
    14. return new Result(ex.getCode(),null,ex.getMessage());
    15. }
    16. //除了自定义的异常处理器,保留对Exception类型的异常处理,用于处理非预期的异常
    17. @ExceptionHandler(Exception.class)
    18. public Result doOtherException(Exception ex){
    19. //记录日志
    20. //发送消息给运维
    21. //发送邮件给开发人员,ex对象发送给开发人员
    22. return new Result(Code.SYSTEM_UNKNOW_ERR,null,"系统繁忙,请稍后再试!");
    23. }
    24. }

  • 相关阅读:
    centos7 install postgres-15
    Spring框架中部署log4j.xml
    LIS系统-实现检验报告集中管理
    相控阵天线有源驻波测试
    引入时间概念的分布式系统浅谈
    【数字电路基础】深入理解setup time和hold time
    NX二次开发-使用SendMessage给NX窗口发送消息最小化
    CesiumJS PrimitiveAPI 高级着色入门 - 从参数化几何与 Fabric 材质到着色器 - 上篇
    AQS介绍
    四、hdfs文件系统基础操作-保姆级教程
  • 原文地址:https://blog.csdn.net/m0_61395860/article/details/133394694