当前位置: 首页 > news >正文

项目训练营第五天

项目训练营第五天

后端代码优化

通用异常处理类编写
@Data
public class BaseResponse<T> implements Serializable {int code;T data;String message = null;String description = null;public BaseResponse(int code, T data, String message, String description) {this.code = code;this.data = data;this.message = message;this.description = description;}public BaseResponse(int code, T data, String message) {this.code = code;this.data = data;this.message = message;this.description = "";}public BaseResponse(int code, T data) {this.code = code;this.data = data;this.message = "";this.description = "";}
}

将之前UserController中各函数的返回值改成泛型BaseResponse对象,便于对原本的返回值进行封装,采用装饰者模式在其外围加上一些参数。

工具类ResultUtils的编写

为了便于修改和返回BaseResponse对象,编写以下ResultUtils函数,对成功和失败的情况进行统一处理

public class ResultUtils {/*** 工具类* @param data* @return* @author yuxin*/public static <T> BaseResponse<T> success(T data) {return new BaseResponse<T>(0, data, "ok");}public  static <T> BaseResponse<T> error(ErrorCode e) {return new BaseResponse<T>(e.getCode(), (T)"", e.getMessage(), e.getDescription());}public  static BaseResponse error(int code, String message, String description) {return new BaseResponse(code, message, description);}
}
枚举值ErrorCode类编写

为了方便对原本的参数值进行封装,编写一个枚举类记录各种常见的错误异常类型

public enum ErrorCode {PARAMS_ERROR(400000, "请求参数错误", ""),PARAMS_NULL(400001, "请求参数为空", ""),NOT_LOGIN(40100,"未登录",""),NO_AUTH(40101, "无权限", ""),SYSTEM_ERROR(50000, "系统内部异常", "");private final int code;private final String message;private final String description;ErrorCode(int code, String message, String description) {this.code = code;this.message = message;this.description = description;}public int getCode() {return code;}public String getMessage() {return message;}public String getDescription() {return description;}
}
  • 三个类分别写在对应名称的类中,放在一个common的文件夹下
  • 测试结果如图:
    在这里插入图片描述
业务异常类编写
@Data
public class BusinessException extends RuntimeException {private int code;private String description;public BusinessException(int code, String description) {this.code = code;this.description = description;}public BusinessException(String message, int code, String description) {super(message);this.code = code;this.description = description;}public BusinessException(String message, Throwable cause, int code, String description) {super(message, cause);this.code = code;this.description = description;}public BusinessException(Throwable cause, int code, String description) {super(cause);this.code = code;this.description = description;}public BusinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, int code, String description) {super(message, cause, enableSuppression, writableStackTrace);this.code = code;this.description = description;}
}
全局异常处理类编写

通过编写全局异常处理类,将内部异常进行封装,不返回给前端代码关于代码结构的异常信息。

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {@ExceptionHandler(BusinessException.class)public BaseResponse BusinessExceptionHandler(BusinessException e) {return ResultUtils.error(e.getCode(), e.getMessage(), "");}@ExceptionHandler(RuntimeException.class)public BaseResponse RuntimeExceptionHandler(RuntimeException e) {return ResultUtils.error(ErrorCode.SYSTEM_ERROR.getCode(), ErrorCode.SYSTEM_ERROR.getMessage(), "");}}
控制层优化
  • 用户查询、删除优化

将原本判断是否为管理员的语句封装,放到业务层中写成一个函数isAdmin

    public boolean isAdmin(HttpServletRequest request) {Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);User user = (User)userObj;if (user == null || user.getUserRole() != 1) {return false;}return true;}
http://www.lryc.cn/news/383397.html

相关文章:

  • 数据收集和数据分析
  • Kubernetes(K8s)从入门到精通系列之十九:Operator模式
  • vuex的actions返回结果类型是promise及actions方法互相调用
  • 【干货】Jupyter Lab操作文档
  • iOS分享到微信,配置Universal Links,并从微信打开app,跳转到指定界面
  • 基于SSM构建的校园失眠与压力管理系统的设计与实现【附源码】
  • SAP 初始化库存移动类型561501511区别简介
  • 情感搞笑聊天记录视频:AI自动化生成技术,操作简单,教程+软件
  • RabbitMQ中lazyqueue队列
  • Java三层框架的解析
  • 算法设计与分析 笔记
  • mapreduce的工作原理
  • vue中v-bind和v-model有什么区别
  • 基于SpringBoot和PostGIS的某国基地可视化实战
  • 为什么Linux服务器空间充足而实际上空间已满的原因
  • 【LC刷题】DAY16:530 501 236
  • Vue 3 的 Teleport 组件实现跨层级通信
  • chromadb
  • Gradle 自动化项目构建-Gradle 核心之 Project
  • 简单介绍 Kamailio cfg_rpc 模块
  • Windows 根据github上的环境需求,安装一个虚拟环境,安装cuda和torch
  • LeetCode 179. 最大数
  • 基于Java+SpringBoot+vue+elementui药品商城采购系统详细设计实现
  • Pikachu靶场--文件上传
  • 突破架构瓶颈:克服软件系统中的漂移和侵蚀
  • 每日练题(py,c,cpp).6_19,6_20
  • 居中显示-css样式
  • 生骨肉冻干喂猫比较好?热门、口碑好、值得入手生骨肉冻干力荐
  • 【安卓13 源码】RescueParty救援机制
  • 详细介绍iutils.dll丢失的多个解决方法,一键快速修复丢失的iutils.dll文件