feat-完善ErrorCode、切面检查、返回结果类

This commit is contained in:
DC_DC 2024-04-22 15:36:42 +08:00
parent 66d02822f2
commit 6070744300
5 changed files with 149 additions and 5 deletions

10
pom.xml
View File

@ -21,6 +21,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
@ -41,6 +45,12 @@
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,30 @@
package cn.dcsy.stsy.aspect;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/**
* @author DC_DC
* Date: 2024/4/22/15:11
*/
@Aspect
@Component
@Slf4j
public class LoggingAspect {
@Before("execution(* cn.dcsy.stsy.controllers.*.*(..))")
public void controllerAspect(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
String methodName = signature.getName();
Object targetObject = joinPoint.getTarget();
Class<?> targetClass = targetObject.getClass();
log.info("[CONTROL] 获取 {} 类的 {} 方法", targetClass.getName(), methodName);
}
}

View File

@ -2,6 +2,7 @@ package cn.dcsy.stsy.controllers;
import cn.dcsy.stsy.service.UserService;
import cn.dcsy.stsy.utils.BaseResponse;
import cn.dcsy.stsy.utils.ResultUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
@ -21,19 +22,18 @@ import org.springframework.web.bind.annotation.*;
@RequiredArgsConstructor
public class BasicController {
private final UserService userService;
/**
* 网站主页
*/
@GetMapping("/index")
public ResponseEntity<BaseResponse> index() {
log.info("访问主页");
BaseResponse response = new BaseResponse("欢迎", 200, "Success", "这是故事管理系统主页");
return ResponseEntity.ok(response);
return ResultUtil.success("访问成功");
}
/*
* 用户登录
* */
* 用户登录
* */
@PostMapping("/login")
public ResponseEntity<BaseResponse> login(@RequestParam String username, @RequestParam String password) {
log.info("尝试登录 用户名: {}", username);

View File

@ -0,0 +1,27 @@
package cn.dcsy.stsy.utils;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* 错误码
* <hr/>
*
* @author DC_DC
* Date: 2024/4/22/14:47
*/
@Getter
@RequiredArgsConstructor
public enum ErrorCode {
REQUEST_BODY_ERROR("RequestBodyError", 40001, "请求体错误"),
REQUEST_PARAM_ERROR("RequestParamError", 40002, "请求参数错误"),
REQUEST_METHOD_ERROR("RequestMethodError", 40003, "请求方法错误"),
REQUEST_HEADER_ERROR("RequestHeaderError", 40004, "请求头错误"),
REQUEST_URL_ERROR("RequestUrlError", 40005, "请求地址错误"),
;
private final String output;
private final Integer code;
private final String message;
}

View File

@ -0,0 +1,77 @@
package cn.dcsy.stsy.utils;
import org.springframework.http.ResponseEntity;
/**
* @author DC_DC
* Date: 2024/4/22/15:19
*/
public class ResultUtil {
/**
* 生成表示操作成功的响应实体不带数据参数
* <hr/>
*
* @param message 成功时的提示消息
* @return 包含成功状态码和消息的响应实体
*/
public static ResponseEntity<BaseResponse> success(String message) {
// 创建并返回一个包含成功代码消息和空错误信息的响应实体
return ResponseEntity
.ok(new BaseResponse("Success", 200, message, null));
}
/**
* 生成表示操作成功的响应实体携带数据参数
* <hr/>
*
* @param message 成功时的提示消息
* @return 包含成功状态码和消息的响应实体
*/
public static ResponseEntity<BaseResponse> success(String message, Object data) {
return ResponseEntity
.ok(new BaseResponse("Success", 200, message, data));
}
/**
* 生成错误响应实体不带数据参数
* <hr/>
*
* @param errorCode 错误码
* @return 返回一个包含错误信息的ResponseEntity对象
*/
public static ResponseEntity<BaseResponse> error(ErrorCode errorCode) {
return ResponseEntity
.status(errorCode.getCode() / 100)
.body(new BaseResponse(errorCode.getOutput(), errorCode.getCode(), errorCode.getMessage(), null));
}
/**
* 生成错误响应实体携带数据参数
*
* @param errorCode 错误码对象包含错误信息状态码和错误输出
* @param data 可选的数据对象包含与错误相关的额外数据
* @return 返回一个包含错误信息的ResponseEntity对象
*/
public static ResponseEntity<BaseResponse> error(ErrorCode errorCode, Object data) {
// 根据错误码设置响应状态码
return ResponseEntity
.status(errorCode.getCode() / 100)
// 构造包含错误信息和数据的响应体
.body(new BaseResponse(errorCode.getOutput(), errorCode.getCode(), errorCode.getMessage(), data));
}
/**
* 生成错误响应实体
*
* @param errorMessage 错误信息描述了具体的错误内容
* @param errorCode 错误码包含了错误的代码和输出信息
* @param data 可选的数据包含了与错误相关的额外数据或信息
* @return 返回一个包含错误信息的ResponseEntity对象
*/
public static ResponseEntity<BaseResponse> error(String errorMessage, ErrorCode errorCode, Object data) {
// 根据错误码设置HTTP响应状态码并构建BaseResponse对象作为响应体
return ResponseEntity
.status(errorCode.getCode() / 100)
.body(new BaseResponse(errorCode.getOutput(), errorCode.getCode(), errorMessage, data));
}
}