commit 963e332c9a98dc39311e60d970ce570ca9749469 Author: zhangsan <646228430@qq.com> Date: Sat Mar 1 09:52:12 2025 +0800 3.1 firtst commit 初始化项目 diff --git a/src/main/java/edu/whut/smilepicturebackend/common/BaseResponse.java b/src/main/java/edu/whut/smilepicturebackend/common/BaseResponse.java new file mode 100644 index 0000000..0a2ce40 --- /dev/null +++ b/src/main/java/edu/whut/smilepicturebackend/common/BaseResponse.java @@ -0,0 +1,34 @@ +package edu.whut.smilepicturebackend.common; +import edu.whut.smilepicturebackend.exception.ErrorCode; +import lombok.Data; + +import java.io.Serializable; + +/** + * 全局响应封装类 + * + * @param + */ +@Data +public class BaseResponse implements Serializable { + + private int code; + + private T data; + + private String message; + + public BaseResponse(int code, T data, String message) { + this.code = code; + this.data = data; + this.message = message; + } + + public BaseResponse(int code, T data) { + this(code, data, ""); + } + + public BaseResponse(ErrorCode errorCode) { + this(errorCode.getCode(), null, errorCode.getMessage()); + } +} diff --git a/src/main/java/edu/whut/smilepicturebackend/common/DeleteRequest.java b/src/main/java/edu/whut/smilepicturebackend/common/DeleteRequest.java new file mode 100644 index 0000000..4b96de1 --- /dev/null +++ b/src/main/java/edu/whut/smilepicturebackend/common/DeleteRequest.java @@ -0,0 +1,19 @@ +package edu.whut.smilepicturebackend.common; + +import lombok.Data; + +import java.io.Serializable; + +/** + * 通用的删除请求类(根据id删除) + */ +@Data +public class DeleteRequest implements Serializable { + + /** + * id + */ + private Long id; + + private static final long serialVersionUID = 1L; +} \ No newline at end of file diff --git a/src/main/java/edu/whut/smilepicturebackend/common/PageRequest.java b/src/main/java/edu/whut/smilepicturebackend/common/PageRequest.java new file mode 100644 index 0000000..79617f4 --- /dev/null +++ b/src/main/java/edu/whut/smilepicturebackend/common/PageRequest.java @@ -0,0 +1,30 @@ +package edu.whut.smilepicturebackend.common; + +import lombok.Data; + +/** + * 通用的分页请求类 + */ +@Data +public class PageRequest { + + /** + * 当前页号 + */ + private int current = 1; + + /** + * 页面大小 + */ + private int pageSize = 10; + + /** + * 排序字段 + */ + private String sortField; + + /** + * 排序顺序(默认降序) + */ + private String sortOrder = "descend"; //descend降序 ascend升序 +} \ No newline at end of file diff --git a/src/main/java/edu/whut/smilepicturebackend/common/ResultUtils.java b/src/main/java/edu/whut/smilepicturebackend/common/ResultUtils.java new file mode 100644 index 0000000..85825cf --- /dev/null +++ b/src/main/java/edu/whut/smilepicturebackend/common/ResultUtils.java @@ -0,0 +1,50 @@ +package edu.whut.smilepicturebackend.common; +import edu.whut.smilepicturebackend.exception.ErrorCode; + +/** + * 响应工具类 + */ +public class ResultUtils { + + /** + * 成功 + * + * @param data 数据 + * @param 数据类型 + * @return 响应 + */ + public static BaseResponse success(T data) { + return new BaseResponse<>(200, data, "success"); + } + + /** + * 失败 + * + * @param errorCode 错误码 + * @return 响应 + */ + public static BaseResponse error(ErrorCode errorCode) { + return new BaseResponse<>(errorCode); + } + + /** + * 失败 + * + * @param code 错误码 + * @param message 错误信息 + * @return 响应 + */ + public static BaseResponse error(int code, String message) { + return new BaseResponse<>(code, null, message); + } + + /** + * 失败 + * + * @param errorCode 错误码 + * @return 响应 + */ + public static BaseResponse error(ErrorCode errorCode, String message) { + return new BaseResponse<>(errorCode.getCode(), null, message); + } +} \ No newline at end of file diff --git a/src/main/java/edu/whut/smilepicturebackend/config/CorsConfig.java b/src/main/java/edu/whut/smilepicturebackend/config/CorsConfig.java new file mode 100644 index 0000000..5883870 --- /dev/null +++ b/src/main/java/edu/whut/smilepicturebackend/config/CorsConfig.java @@ -0,0 +1,25 @@ +package edu.whut.smilepicturebackend.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +/** + * 全局跨域配置 + */ +@Configuration +public class CorsConfig implements WebMvcConfigurer { + + @Override + public void addCorsMappings(CorsRegistry registry) { + // 覆盖所有请求 + registry.addMapping("/**") + // 允许发送 Cookie + .allowCredentials(true) + // 放行哪些域名(必须用 patterns,否则 * 会和 allowCredentials 冲突) + .allowedOriginPatterns("*") + .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") + .allowedHeaders("*") + .exposedHeaders("*"); + } +} \ No newline at end of file diff --git a/src/main/java/edu/whut/smilepicturebackend/controller/MainController.java b/src/main/java/edu/whut/smilepicturebackend/controller/MainController.java new file mode 100644 index 0000000..818221e --- /dev/null +++ b/src/main/java/edu/whut/smilepicturebackend/controller/MainController.java @@ -0,0 +1,20 @@ +package edu.whut.smilepicturebackend.controller; +import edu.whut.smilepicturebackend.common.BaseResponse; +import edu.whut.smilepicturebackend.common.ResultUtils; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/") +public class MainController { + + /** + * 健康检查 + */ + @GetMapping("/health") + public BaseResponse health() { + return ResultUtils.success("ok"); + } + +} diff --git a/src/main/java/edu/whut/smilepicturebackend/exception/BusinessException.java b/src/main/java/edu/whut/smilepicturebackend/exception/BusinessException.java new file mode 100644 index 0000000..0aaaf0f --- /dev/null +++ b/src/main/java/edu/whut/smilepicturebackend/exception/BusinessException.java @@ -0,0 +1,31 @@ +package edu.whut.smilepicturebackend.exception; + +import lombok.Getter; + +/** + * 自定义业务异常,继承运行时异常 + */ +@Getter +public class BusinessException extends RuntimeException { + + /** + * 错误码 + */ + private final int code; + + public BusinessException(int code, String message) { + super(message); + this.code = code; + } + + public BusinessException(ErrorCode errorCode) { + super(errorCode.getMessage()); + this.code = errorCode.getCode(); + } + + public BusinessException(ErrorCode errorCode, String message) { + super(message); + this.code = errorCode.getCode(); + } + +} diff --git a/src/main/java/edu/whut/smilepicturebackend/exception/ErrorCode.java b/src/main/java/edu/whut/smilepicturebackend/exception/ErrorCode.java new file mode 100644 index 0000000..f9af563 --- /dev/null +++ b/src/main/java/edu/whut/smilepicturebackend/exception/ErrorCode.java @@ -0,0 +1,32 @@ +package edu.whut.smilepicturebackend.exception; + +import lombok.Getter; + +@Getter +public enum ErrorCode { + + SUCCESS(0, "ok"), + PARAMS_ERROR(40000, "请求参数错误"), + NOT_LOGIN_ERROR(40100, "未登录"), + NO_AUTH_ERROR(40101, "无权限"), + FORBIDDEN_ERROR(40300, "禁止访问"), + NOT_FOUND_ERROR(40400, "请求数据不存在"), + SYSTEM_ERROR(50000, "系统内部异常"), + OPERATION_ERROR(50001, "操作失败"); + + /** + * 状态码 + */ + private final int code; + + /** + * 信息 + */ + private final String message; + + ErrorCode(int code, String message) { + this.code = code; + this.message = message; + } + +} \ No newline at end of file diff --git a/src/main/java/edu/whut/smilepicturebackend/exception/GlobalExceptionHandler.java b/src/main/java/edu/whut/smilepicturebackend/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..dc14c1f --- /dev/null +++ b/src/main/java/edu/whut/smilepicturebackend/exception/GlobalExceptionHandler.java @@ -0,0 +1,27 @@ +package edu.whut.smilepicturebackend.exception; + +import edu.whut.smilepicturebackend.common.BaseResponse; +import edu.whut.smilepicturebackend.common.ResultUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +/** + * 全局异常处理器 + */ +@RestControllerAdvice +@Slf4j +public class GlobalExceptionHandler { + + @ExceptionHandler(BusinessException.class) + public BaseResponse businessExceptionHandler(BusinessException e) { + log.error("BusinessException", e); + return ResultUtils.error(e.getCode(), e.getMessage()); + } + + @ExceptionHandler(RuntimeException.class) + public BaseResponse businessExceptionHandler(RuntimeException e) { + log.error("RuntimeException", e); + return ResultUtils.error(ErrorCode.SYSTEM_ERROR, e.getMessage()); + } +} diff --git a/src/main/java/edu/whut/smilepicturebackend/exception/ThrowUtils.java b/src/main/java/edu/whut/smilepicturebackend/exception/ThrowUtils.java new file mode 100644 index 0000000..488d350 --- /dev/null +++ b/src/main/java/edu/whut/smilepicturebackend/exception/ThrowUtils.java @@ -0,0 +1,42 @@ +package edu.whut.smilepicturebackend.exception; + +/** + * 异常处理工具类 + */ +public class ThrowUtils { + + /** + * 条件成立则抛异常 + * + * @param condition 条件 + * @param runtimeException 异常 + */ + public static void throwIf(boolean condition, RuntimeException runtimeException) { + if (condition) { + throw runtimeException; + } + } + + /** + * 条件成立则抛异常 + * + * @param condition 条件 + * @param errorCode 错误码 + */ + public static void throwIf(boolean condition, ErrorCode errorCode) { + throwIf(condition, new BusinessException(errorCode)); + } + + /** + * 条件成立则抛异常 + * + * @param condition 条件 + * @param errorCode 错误码 + * @param message 错误信息 + */ + //可以自己指定错误消息,而不是中的 + public static void throwIf(boolean condition, ErrorCode errorCode, String message) { + throwIf(condition, new BusinessException(errorCode, message)); + } + +}