grtsinry43 0f5defb2d0
Refactor project structure and enhance functionality
- Renamed XML mapper files for consistency.
- Added new fields `is_admin` and `is_banned` to the `Reader` entity and updated the database schema.
- Introduced `AuthCheck` annotation for method-level authorization.
- Implemented JWT utility for token generation and validation.
- Enhanced service and controller layers for `Book`, `Reader`, and `Orders` with new methods and improved error handling.
- Added global exception handling for better API response management.
- Updated `pom.xml` to include new dependencies for JWT and Spring Security.
2025-05-19 19:05:43 +08:00

71 lines
3.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.grtsinry43.bookmanagement.controller;
import com.grtsinry43.bookmanagement.common.ApiResponse;
import com.grtsinry43.bookmanagement.common.BusinessException;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
/**
* @author grtsinry43
* @date 2024/7/13 上午12:29
* @description 规定触发异常时的返回格式依然是HTTP状态码200只会根据code字段和msg来提供错误信息
*/
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
/**
* 处理缺少请求参数异常
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
public ResponseEntity<ApiResponse<Object>> handleMissingServletRequestParameterException(Exception ex) {
String errorMessage = "缺少请求参数:" + ex.getMessage();
ApiResponse<Object> apiResponse = ApiResponse.error(400, errorMessage);
return new ResponseEntity<>(apiResponse, HttpStatus.OK);
}
/**
* 处理参数校验异常(注释声明即可)
*/
@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<ApiResponse<Object>> handleConstraintViolationException(ConstraintViolationException ex) {
String errorMessage = ex.getMessage();
ApiResponse<Object> apiResponse = ApiResponse.error(400, errorMessage);
return new ResponseEntity<>(apiResponse, HttpStatus.OK);
}
/**
* 处理参数校验异常(注释声明即可)
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ApiResponse<Object>> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
ApiResponse<Object> apiResponse = ApiResponse.error(400, "参数传递有误或者缺少参数");
return new ResponseEntity<>(apiResponse, HttpStatus.OK);
}
/**
* 处理业务逻辑异常(注释声明即可)
*/
@ExceptionHandler(BusinessException.class)
public ResponseEntity<ApiResponse<Object>> handleBusinessException(BusinessException ex) {
String errorMessage = ex.getMessage();
ApiResponse<Object> apiResponse = ApiResponse.error(ex.getErrorCode().getCode(), errorMessage);
return new ResponseEntity<>(apiResponse, HttpStatus.OK);
}
/**
* 处理其他异常(注释声明即可)
*/
@ExceptionHandler(Exception.class)
public ResponseEntity<ApiResponse<Object>> handleException(Exception ex) {
String errorMessage = ex.getMessage();
log.error("系统异常: {}", errorMessage, ex); // 使用日志框架记录异常,包含完整堆栈
ApiResponse<Object> apiResponse = ApiResponse.error(500, "服务器发生内部错误,您可以尝试刷新,如果问题依旧,请联系我们");
return new ResponseEntity<>(apiResponse, HttpStatus.OK);
}
}