feat-完善ErrorCode、切面检查、返回结果类
This commit is contained in:
parent
66d02822f2
commit
6070744300
10
pom.xml
10
pom.xml
@ -21,6 +21,10 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
@ -41,6 +45,12 @@
|
|||||||
<groupId>com.mysql</groupId>
|
<groupId>com.mysql</groupId>
|
||||||
<artifactId>mysql-connector-j</artifactId>
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains</groupId>
|
||||||
|
<artifactId>annotations</artifactId>
|
||||||
|
<version>24.1.0</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
30
src/main/java/cn/dcsy/stsy/aspect/LoggingAspect.java
Normal file
30
src/main/java/cn/dcsy/stsy/aspect/LoggingAspect.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@ package cn.dcsy.stsy.controllers;
|
|||||||
|
|
||||||
import cn.dcsy.stsy.service.UserService;
|
import cn.dcsy.stsy.service.UserService;
|
||||||
import cn.dcsy.stsy.utils.BaseResponse;
|
import cn.dcsy.stsy.utils.BaseResponse;
|
||||||
|
import cn.dcsy.stsy.utils.ResultUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@ -21,14 +22,13 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class BasicController {
|
public class BasicController {
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 网站主页
|
* 网站主页
|
||||||
*/
|
*/
|
||||||
@GetMapping("/index")
|
@GetMapping("/index")
|
||||||
public ResponseEntity<BaseResponse> index() {
|
public ResponseEntity<BaseResponse> index() {
|
||||||
log.info("访问主页");
|
return ResultUtil.success("访问成功");
|
||||||
BaseResponse response = new BaseResponse("欢迎", 200, "Success", "这是故事管理系统主页");
|
|
||||||
return ResponseEntity.ok(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
27
src/main/java/cn/dcsy/stsy/utils/ErrorCode.java
Normal file
27
src/main/java/cn/dcsy/stsy/utils/ErrorCode.java
Normal 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;
|
||||||
|
}
|
77
src/main/java/cn/dcsy/stsy/utils/ResultUtil.java
Normal file
77
src/main/java/cn/dcsy/stsy/utils/ResultUtil.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user