feat: 完善后端架构 - 标准化Controller层和Service层
✨ 新功能: - 创建了完整的Service层架构,包含所有业务实体的Service接口和实现类 - 新增8个标准化的Controller类,支持完整的CRUD操作 - 实现了统一的Request/Response模式和分页查询功能 - 创建了认证服务(AuthService)和令牌服务(TokenService) - 添加了Redis配置和认证拦截器 🏗️ 架构优化: - 移除Controller层所有try-catch块,使用全局异常处理机制 - 创建了专门的异常类(AuthException, TokenException, CaptchaException) - 统一了API返回格式,完善了Result类的方法 - 实现了标准的分页查询和参数校验 📦 新增文件: - 8个Controller类: Achievement, Comment, CommunityPost, Conversation, CozeApiCall, EmotionAnalysis, Reward, UserStats - 12个Service接口和对应的实现类 - 标准化的DTO类(Request/Response) - 异常处理类和拦截器 - 测试用例 🔧 重构优化: - 重写了AuthController,移除所有业务逻辑到Service层 - 优化了MessageController,使用标准的Request/Response格式 - 更新了全局异常处理器,支持多种异常类型 - 完善了WebConfig配置,添加认证拦截器 📊 代码统计: - 新增文件: 60+个 - 新增代码行数: 8000+行 - 重构代码行数: 1000+行 - 移除过时接口: 4个
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package com.emotion.exception;
|
||||
|
||||
/**
|
||||
* 认证异常类
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
public class AuthException extends BusinessException {
|
||||
|
||||
public AuthException(String message) {
|
||||
super(401, message);
|
||||
}
|
||||
|
||||
public AuthException(String message, Throwable cause) {
|
||||
super(401, message, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.emotion.exception;
|
||||
|
||||
/**
|
||||
* 业务异常
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
public class BusinessException extends RuntimeException {
|
||||
|
||||
private Integer code;
|
||||
|
||||
public BusinessException(String message) {
|
||||
super(message);
|
||||
this.code = 500;
|
||||
}
|
||||
|
||||
public BusinessException(Integer code, String message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public BusinessException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.code = 500;
|
||||
}
|
||||
|
||||
public BusinessException(Integer code, String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.emotion.exception;
|
||||
|
||||
/**
|
||||
* 验证码异常类
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
public class CaptchaException extends BusinessException {
|
||||
|
||||
public CaptchaException(String message) {
|
||||
super(400, message);
|
||||
}
|
||||
|
||||
public CaptchaException(String message, Throwable cause) {
|
||||
super(400, message, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package com.emotion.exception;
|
||||
|
||||
import com.emotion.common.Result;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 全局异常处理器
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||
|
||||
/**
|
||||
* 处理认证异常
|
||||
*/
|
||||
@ExceptionHandler(AuthException.class)
|
||||
public Result<Void> handleAuthException(AuthException e, HttpServletRequest request) {
|
||||
log.warn("认证异常: {} {} - {}", request.getMethod(), request.getRequestURI(), e.getMessage());
|
||||
return Result.unauthorized(e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理令牌异常
|
||||
*/
|
||||
@ExceptionHandler(TokenException.class)
|
||||
public Result<Void> handleTokenException(TokenException e, HttpServletRequest request) {
|
||||
log.warn("令牌异常: {} {} - {}", request.getMethod(), request.getRequestURI(), e.getMessage());
|
||||
return Result.unauthorized(e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理验证码异常
|
||||
*/
|
||||
@ExceptionHandler(CaptchaException.class)
|
||||
public Result<Void> handleCaptchaException(CaptchaException e, HttpServletRequest request) {
|
||||
log.warn("验证码异常: {} {} - {}", request.getMethod(), request.getRequestURI(), e.getMessage());
|
||||
return Result.badRequest(e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理业务异常
|
||||
*/
|
||||
@ExceptionHandler(BusinessException.class)
|
||||
public Result<Void> handleBusinessException(BusinessException e, HttpServletRequest request) {
|
||||
log.warn("业务异常: {} {} - {}", request.getMethod(), request.getRequestURI(), e.getMessage());
|
||||
|
||||
if (e.getCode() == 401) {
|
||||
return Result.unauthorized(e.getMessage());
|
||||
} else if (e.getCode() == 403) {
|
||||
return Result.forbidden(e.getMessage());
|
||||
} else if (e.getCode() == 400) {
|
||||
return Result.badRequest(e.getMessage());
|
||||
} else {
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理参数校验异常
|
||||
*/
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public Result<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) {
|
||||
log.warn("参数校验失败: {} {}", request.getMethod(), request.getRequestURI(), e);
|
||||
|
||||
StringBuilder message = new StringBuilder("参数校验失败: ");
|
||||
for (FieldError error : e.getBindingResult().getFieldErrors()) {
|
||||
message.append(error.getField()).append(" ").append(error.getDefaultMessage()).append("; ");
|
||||
}
|
||||
|
||||
return Result.badRequest(message.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理Bean校验异常
|
||||
*/
|
||||
@ExceptionHandler(BindException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public Result<Void> handleBindException(BindException e, HttpServletRequest request) {
|
||||
log.warn("参数绑定失败: {} {}", request.getMethod(), request.getRequestURI(), e);
|
||||
|
||||
StringBuilder message = new StringBuilder("参数绑定失败: ");
|
||||
for (FieldError error : e.getBindingResult().getFieldErrors()) {
|
||||
message.append(error.getField()).append(" ").append(error.getDefaultMessage()).append("; ");
|
||||
}
|
||||
|
||||
return Result.badRequest(message.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理约束校验异常
|
||||
*/
|
||||
@ExceptionHandler(ConstraintViolationException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public Result<Void> handleConstraintViolationException(ConstraintViolationException e, HttpServletRequest request) {
|
||||
log.warn("约束校验失败: {} {}", request.getMethod(), request.getRequestURI(), e);
|
||||
|
||||
StringBuilder message = new StringBuilder("约束校验失败: ");
|
||||
Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
|
||||
for (ConstraintViolation<?> violation : violations) {
|
||||
message.append(violation.getPropertyPath()).append(" ").append(violation.getMessage()).append("; ");
|
||||
}
|
||||
|
||||
return Result.badRequest(message.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理非法参数异常
|
||||
*/
|
||||
@ExceptionHandler(IllegalArgumentException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public Result<Void> handleIllegalArgumentException(IllegalArgumentException e, HttpServletRequest request) {
|
||||
log.warn("非法参数: {} {}", request.getMethod(), request.getRequestURI(), e);
|
||||
return Result.badRequest("参数错误: " + e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理空指针异常
|
||||
*/
|
||||
@ExceptionHandler(NullPointerException.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public Result<Void> handleNullPointerException(NullPointerException e, HttpServletRequest request) {
|
||||
log.error("空指针异常: {} {}", request.getMethod(), request.getRequestURI(), e);
|
||||
return Result.error("系统内部错误");
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理运行时异常
|
||||
*/
|
||||
@ExceptionHandler(RuntimeException.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public Result<Void> handleRuntimeException(RuntimeException e, HttpServletRequest request) {
|
||||
log.error("运行时异常: {} {}", request.getMethod(), request.getRequestURI(), e);
|
||||
return Result.error("系统运行异常: " + e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理所有其他异常
|
||||
*/
|
||||
@ExceptionHandler(Exception.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public Result<Void> handleException(Exception e, HttpServletRequest request) {
|
||||
log.error("未知异常: {} {}", request.getMethod(), request.getRequestURI(), e);
|
||||
return Result.error("系统异常,请联系管理员");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.emotion.exception;
|
||||
|
||||
/**
|
||||
* 令牌异常类
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
public class TokenException extends AuthException {
|
||||
|
||||
public TokenException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public TokenException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user