- 将Spring Boot版本从4.0.0升级到3.2.0 - 更新mt-pay模块的依赖配置,使用spring-boot-starter-web替代webmvc - 在应用启动时添加醒目的ASCII艺术风格日志输出 - 添加前端访问地址配置项app.frontend.url - 优化全局异常处理器返回的数据结构 - 修改CreateProductRequestDTO以支持多个主图URL - 调整MyBatis Plus查询构造方式为显式LambdaQueryWrapper - 更新Maven插件配置,跳过测试执行 - 修复XML配置中的特殊字符转义问题 - 统一服务端口为8082并在启动日志中显示完整访问信息
82 lines
3.0 KiB
Java
82 lines
3.0 KiB
Java
package com.mtkj.mtpay.exception;
|
||
|
||
import com.mtkj.mtpay.common.Result;
|
||
import com.mtkj.mtpay.common.ResultCode;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.http.ResponseEntity;
|
||
import org.springframework.validation.FieldError;
|
||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||
|
||
import java.util.HashMap;
|
||
import java.util.Map;
|
||
|
||
/**
|
||
* 全局异常处理器
|
||
*/
|
||
@Slf4j
|
||
@RestControllerAdvice
|
||
public class GlobalExceptionHandler {
|
||
|
||
/**
|
||
* 处理业务异常
|
||
*/
|
||
@ExceptionHandler(BusinessException.class)
|
||
public ResponseEntity<Result<Object>> handleBusinessException(BusinessException e) {
|
||
log.warn("业务异常: {}", e.getMessage());
|
||
return ResponseEntity.ok(Result.fail(e.getCode(), e.getMessage()));
|
||
}
|
||
|
||
/**
|
||
* 处理参数验证异常
|
||
*/
|
||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||
public ResponseEntity<Result<Map<String, String>>> handleValidationException(MethodArgumentNotValidException e) {
|
||
Map<String, String> errors = new HashMap<>();
|
||
e.getBindingResult().getAllErrors().forEach(error -> {
|
||
String fieldName = ((FieldError) error).getField();
|
||
String errorMessage = error.getDefaultMessage();
|
||
errors.put(fieldName, errorMessage);
|
||
});
|
||
|
||
log.warn("参数验证失败: {}", errors);
|
||
Result<Map<String, String>> result = Result.fail(ResultCode.VALIDATION_ERROR.getCode(), ResultCode.VALIDATION_ERROR.getMessage());
|
||
result.setData(errors);
|
||
return ResponseEntity.badRequest().body(result);
|
||
}
|
||
|
||
/**
|
||
* 处理文件上传大小超限异常
|
||
*/
|
||
@ExceptionHandler(MaxUploadSizeExceededException.class)
|
||
public ResponseEntity<Result<Object>> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
|
||
log.warn("文件上传大小超限: {}", e.getMessage());
|
||
String message = "文件大小超过限制,单个文件最大10MB,请压缩后重试";
|
||
return ResponseEntity.badRequest()
|
||
.body(Result.fail(ResultCode.PARAM_ERROR.getCode(), message));
|
||
}
|
||
|
||
/**
|
||
* 处理运行时异常
|
||
*/
|
||
@ExceptionHandler(RuntimeException.class)
|
||
public ResponseEntity<Result<Object>> handleRuntimeException(RuntimeException e) {
|
||
log.error("运行时异常", e);
|
||
return ResponseEntity.internalServerError()
|
||
.body(Result.fail(ResultCode.SYSTEM_ERROR.getCode(), e.getMessage()));
|
||
}
|
||
|
||
/**
|
||
* 处理其他异常
|
||
*/
|
||
@ExceptionHandler(Exception.class)
|
||
public ResponseEntity<Result<Object>> handleException(Exception e) {
|
||
log.error("系统异常", e);
|
||
return ResponseEntity.internalServerError()
|
||
.body(Result.fail(ResultCode.SYSTEM_ERROR));
|
||
}
|
||
}
|
||
|