【Springboot学习】从零开始学习Springboot(九)

异常处理

使用异常处理器进行异常处理

@RestControllerAdvice
public class ProjectExceptionAdvice {
    @ExceptionHandler
    public R doException(Exception ex){
        ex.printStackTrace();
        return new R("服务器故障");
    }

}

 业务一致性处理

在前端进行消息处理

handleAdd() {

    axios.post("/books", this.formData).then(res => {
        if (res.data.flag) {
            this.dialogFormVisible = false
            this.$message.success(res.data.msg)
        } else {
            this.$message.error(res.data.msg)
        }
    }).finally(() => {
        this.getAll()
    })
}

在后端(表现层)进行消息处理

@PostMapping
public R insert(@RequestBody Book book) throws IOException {
    
    Boolean flag = bookService.insertBook(book);
    return new R(flag, flag ? "添加成功" : "添加失败");
}

 

posted @ 2022-07-14 18:15  Texley  阅读(32)  评论(0)    收藏  举报