Initial commit

This commit is contained in:
wangziqi
2026-02-09 09:51:14 -08:00
commit a7ce0a089e
104 changed files with 6470 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
package com.maternalmall.common;
import jakarta.validation.ConstraintViolationException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(BizException.class)
public ApiResponse<Void> handleBiz(BizException ex) {
return ApiResponse.fail(ex.getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ApiResponse<Void> handleValid(MethodArgumentNotValidException ex) {
String msg = ex.getBindingResult().getFieldErrors().stream().findFirst()
.map(e -> e.getField() + ":" + e.getDefaultMessage()).orElse("参数错误");
return ApiResponse.fail(msg);
}
@ExceptionHandler(ConstraintViolationException.class)
public ApiResponse<Void> handleConstraint(ConstraintViolationException ex) {
return ApiResponse.fail(ex.getMessage());
}
@ExceptionHandler(Exception.class)
public ApiResponse<Void> handleUnknown(Exception ex) {
return ApiResponse.fail(ex.getMessage());
}
}