SSM整合——表现层(返回数据封装、异常处理)

本文档详细介绍了Spring Boot应用中表现层的设计,包括数据封装、自定义状态码、控制器以及异常处理器的实现。通过自定义Result类进行数据返回,定义了系统级和业务级异常,并在服务层捕获异常,使用@RestControllerAdvice进行全局异常处理。
摘要由CSDN通过智能技术生成

目录

表现层目录结构​

1.数据封装

1.1 自定义Result类

1.2 自定义状态码

1.3 控制器类

2.异常处理器

2.1 自定义异常(系统级、业务级)

2.2 自定义异常编码(持续补充)

2.3 触发自定义异常

2.4 拦截并处理异常


表现层目录结构 

1.数据封装

1.1自定义Result类

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

    public Result() {
    }

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

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

    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 getMessage() {
        return message;
    }

    public void setMessage(String massage) {
        this.message = massage;
    }
}

 1.2 自定义状态码

public class Code {
    public static final Integer ADD_OK = 20011;
    public static final Integer DELETE_0K = 20021;
    public static final Integer UPDATE_OK = 20031;
    public static final Integer GET_OK = 20041;

    public static final Integer ADD_ERR = 20010;
    public static final Integer DELETE_ERR = 20020;
    public static final Integer UPDATE_ERR = 20030;
    public static final Integer GET_ERR = 20048;

}

1.3控制器类

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


    @PostMapping
    public Result add(@RequestBody Book book) {
        boolean flag = bookService.add(book);
        return new Result(flag ? Code.ADD_OK : Code.ADD_ERR, flag);
    }

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

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

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Book book = bookService.getById(id);
        Integer code = book != null ? Code.GET_OK : Code.GET_ERR;
        String massage = book != null ? "" : "数据查询失败,请重试";
        return new Result(code, book, massage);
    }

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

2.异常处理器

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

2.1自定义异常(系统级、业务级)

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;
    }

}




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;
    }

}

2.2自定义异常编码(持续补充)

   public class Code {
    public static final Integer SYSTEM_ERR = 50001;
    public static final Integer SYSTEM_TIMEOUT_ERR = 50002;
    public static final Integer SYSTEM_UNKNOWN_ERR = 59999;
    public static final Integer BUSINESS_ERR = 60002;

}

2.3 触发自定义异常

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;

    @Override
    public boolean add(Book book) {
        try {
            int add = bookDao.add(book);
            return add > 0;
        } catch (Exception e) {
            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR, "服务器异常", e);
        }
    }

    @Override
    public boolean update(Book book) {
        try {
            int update = bookDao.update(book);
            return update > 0;
        } catch (Exception e) {
            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR, "服务器异常", e);
        }
    }

    @Override
    public boolean delete(Integer id) {
        if (id <= 0) {
            throw new BusinessException(Code.BUSINESS_ERR, "无效的id");
        }
        try {
            int delete = bookDao.delete(id);
            return delete > 0;
        } catch (Exception e) {
            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR, "服务器异常", e);
        }
    }

    @Override
    public Book getById(Integer id) {
        if (id <= 0) {
            throw new BusinessException(Code.BUSINESS_ERR, "无效的id");
        }
        try {
            return bookDao.getById(id);
        } catch (Exception e) {
            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR, "服务器异常", e);
        }
    }

    @Override
    public List<Book> getAll() {
        try {
            return bookDao.getAll();
        } catch (Exception e) {
            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR, "服务器异常", e);
        }
    }
}

2.4拦截并处理异常

@RestControllerAdvice
public class ProjectExceptionAdvice {

    //系统异常处理
    @ExceptionHandler(SystemException.class)//定义处理哪一种异常
    public Result doSystemException(SystemException ex) {
        //记录日志
        //发送消息给运维
        //发送邮件给开发人员
        return new Result(ex.getCode(), null, ex.getMessage());
    }


    //业务异常处理
    @ExceptionHandler(BusinessException.class)//定义处理哪一种异常
    public Result doBusinessException(BusinessException ex) {
        return new Result(ex.getCode(), null, ex.getMessage());
    }

    //其他异常处理
    @ExceptionHandler(Exception.class)//定义处理哪一种异常
    public Result doException(BusinessException ex) {
        //记录日志
        //发送消息给运维
        //发送邮件给开发人员
        return new Result(Code.SYSTEM_UNKNOWN_ERR, null, "系统繁忙,请稍后再试");
    }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值