SpringMVC第五天(SSM整合)

表现层数据封装

设置统一数据返回结果类

java 复制代码
public class Result{
    private Object data;
    private Integer code;
    private String msg;


}

设置统一数据返回结果编码

java 复制代码
package com.cacb.controller;

public class Code {
    public static final Integer SAVE_OK = 20011;
    public static final Integer DELETE_OK = 20021;
    public static final Integer UPDATE_OK = 20031;
    public static final Integer GET_OK = 20041;

    public static final Integer SAVE_ERR = 20010;
    public static final Integer DELETE_ERR = 20020;
    public static final Integer UPDATE_ERR = 20030;
    public static final Integer GET_ERR = 20040;
}

根据情况设定合理的Result

java 复制代码
package com.cacb.controller;

public class Result {
    private Object data;
    private Integer code;
    private String msg;

    public Result(Integer code ,Object data, String msg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }

    public Result(Integer code ,Object data) {
        this.data = data;
        this.code = code;
    }

    public Result() {
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = 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;
    }
}
java 复制代码
package com.cacb.controller;

import com.cacb.domain.Book;
import com.cacb.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.cacb.controller.Code.*;

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;

    @PostMapping
    public Result save(@RequestBody Book book) {
        boolean flag = bookService.save(book);
        return new Result(flag?SAVE_OK:SAVE_ERR,flag);
    }

    @PutMapping
    public Result update(@RequestBody Book book) {
        boolean flag = bookService.update(book);
        return new Result(flag?UPDATE_OK:UPDATE_ERR,flag);
    }

    @DeleteMapping( "/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = bookService.delete(id);
        return new Result(flag?DELETE_OK:DELETE_ERR,flag);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {

        Book book = bookService.getById(id);
        Integer code = book != null ? GET_OK:GET_ERR;
        String msg = book != null ?  "" : "数据查询失败";
        return new Result(code,book,msg)  ;
    }

    @GetMapping
    public Result getAll() {
        List<Book> bookList = bookService.getAll();
        Integer code = bookList != null ? GET_OK:GET_ERR;
        String msg = bookList != null ?  "" : "数据查询失败";
        return new Result(code,bookList,msg)  ;
    }
}

异常处理器

出现异常常见位置与常见诱因如下

1.框架内部抛出的异常:因使用不合规导致

2.数据层抛出的异常:因为外部服务器故障导致(例如:服务器访问超时)

3.业务层抛出的异常:因业务逻辑书写错误导致(例如:不匹配的数据类型间导致异常)

4.表现层抛出的异常:因数据收集、校验等规则导致(例如:不匹配的数据类型间导致异常)

5.工具类抛出的异常:因工具类书写不严谨不够健壮导致(例如:必要时放的连接长期未释放等)

各层均会出现异常,为集中处理各层所有的异常

应该: 所有的异常均抛出到表现层进行处理

异常处理器

集中的、统一的处理项目中出现的异常

java 复制代码
package com.cacb.controller;


import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ProjectExceptionAdvice {
    @ExceptionHandler(Exception.class)
    public Result doException(Exception ex){
        System.out.println("处理异常");
        return new Result(1111,null);
    }
}

项目异常处理方案

项目异常分类

业务异常(BusinessException)

规范的用户行为产生的异常

不规范的用户行为操作产生的异常

(发送对应消息传递给用户,提醒规范操作)

系统异常(SystemException)

项目运行过程中可预计且无法避免的异常

(发送固定信息传递给用户,安抚用户)

(发送特定消息给运维人员,提醒维护)

(记录日志)

其他异常(Exception)

编程人员未预期到的异常

(发送固定信息传递给用户,安抚用户)

(发送特定消息给运维人员,提醒维护)

(纳入预期范围内)

(记录日志)

处理步骤

第一步、自定义项目系统级异常

java 复制代码
package com.cacb.exception;

public class SystemException extends RuntimeException{
    private Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public SystemException(Integer code, String message ) {
        super(message);
        this.code = code;
    }

    public SystemException(  Integer code,String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }


}

第二步、自定义项目业务级异常

java 复制代码
package com.cacb.exception;

public class BusinessException extends RuntimeException{
    private Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public BusinessException(Integer code, String message ) {
        super(message);
        this.code = code;
    }

    public BusinessException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }


}

第三步、自定义异常编码

java 复制代码
    public static final Integer SYSTEM_ERR = 50001;
    public static final Integer BUSINESS_ERR = 50002;
    public static final Integer SYSTEM_UNKNOW_ERR = 59999;

第四步、触发自定义异常

java 复制代码
    @Override
    public boolean delete(Integer id) {
        if (id < 0)
        {
            throw new BusinessException(Code.BUSINESS_ERR,"输入ID非法!");
        }
        bookDao.delete(id);
        return true;
    }

第五步、拦截并处理异常

java 复制代码
package com.cacb.controller;


import com.cacb.exception.BusinessException;
import com.cacb.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static com.cacb.controller.Code.SYSTEM_UNKNOW_ERR;

@RestControllerAdvice
public class             ProjectExceptionAdvice {
    @ExceptionHandler(SystemException.class)
    public Result doException(SystemException ex){
        //记录日志
        //发消息给运维
        //发送邮件给开哦发人员,ex对象发送给2开发人员
        return new Result(ex.getCode(),null,ex.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    public Result doException(BusinessException ex){
        //记录日志
        //发消息给运维
        //发送邮件给开哦发人员,ex对象发送给2开发人员
        return new Result(ex.getCode(),null,ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result doException(Exception ex){
        return new Result(SYSTEM_UNKNOW_ERR,null,ex.getMessage());
    }
}
相关推荐
汤姆和杰瑞在瑞士吃糯米粑粑8 分钟前
【C++学习篇】AVL树
开发语言·c++·学习
J不A秃V头A13 分钟前
IntelliJ IDEA中设置激活的profile
java·intellij-idea
Biomamba生信基地15 分钟前
R语言基础| 功效分析
开发语言·python·r语言·医药
DARLING Zero two♡15 分钟前
【优选算法】Pointer-Slice:双指针的算法切片(下)
java·数据结构·c++·算法·leetcode
手可摘星河17 分钟前
php中 cli和cgi的区别
开发语言·php
小池先生27 分钟前
springboot启动不了 因一个spring-boot-starter-web底下的tomcat-embed-core依赖丢失
java·spring boot·后端
CodeClimb30 分钟前
【华为OD-E卷-木板 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
odng33 分钟前
IDEA自己常用的几个快捷方式(自己的习惯)
java·ide·intellij-idea
CT随41 分钟前
Redis内存碎片详解
java·开发语言
brrdg_sefg1 小时前
gitlab代码推送
java