|
@ -3,9 +3,11 @@ package com.iflytop.gd.common.handler; |
|
|
import com.iflytop.gd.common.exception.AppException; |
|
|
import com.iflytop.gd.common.exception.AppException; |
|
|
import com.iflytop.gd.common.result.Result; |
|
|
import com.iflytop.gd.common.result.Result; |
|
|
import com.iflytop.gd.common.result.ResultCode; |
|
|
import com.iflytop.gd.common.result.ResultCode; |
|
|
|
|
|
import jakarta.servlet.http.HttpServletRequest; |
|
|
import jakarta.validation.ConstraintViolationException; |
|
|
import jakarta.validation.ConstraintViolationException; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.validation.BindException; |
|
|
import org.springframework.validation.BindException; |
|
|
|
|
|
import org.springframework.web.HttpRequestMethodNotSupportedException; |
|
|
import org.springframework.web.bind.MethodArgumentNotValidException; |
|
|
import org.springframework.web.bind.MethodArgumentNotValidException; |
|
|
import org.springframework.web.bind.annotation.ExceptionHandler; |
|
|
import org.springframework.web.bind.annotation.ExceptionHandler; |
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice; |
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice; |
|
@ -16,6 +18,20 @@ import java.util.stream.Collectors; |
|
|
@RestControllerAdvice |
|
|
@RestControllerAdvice |
|
|
public class GlobalExceptionHandler { |
|
|
public class GlobalExceptionHandler { |
|
|
|
|
|
|
|
|
|
|
|
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) |
|
|
|
|
|
public Result<?> handleMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpServletRequest request) { |
|
|
|
|
|
String method = request.getMethod(); |
|
|
|
|
|
String uri = request.getRequestURI(); |
|
|
|
|
|
String allowed = ex.getSupportedHttpMethods() != null |
|
|
|
|
|
? ex.getSupportedHttpMethods().toString() |
|
|
|
|
|
: "[]"; |
|
|
|
|
|
String msg = String.format( |
|
|
|
|
|
"请求方法 '%s' 不支持 (URL: %s)。支持的方法有:%s", |
|
|
|
|
|
method, uri, allowed); |
|
|
|
|
|
log.error(msg, ex); |
|
|
|
|
|
return Result.failed(ResultCode.METHOD_NOT_ALLOWED.getCode(), msg); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 1) JSON Body 校验失败 |
|
|
// 1) JSON Body 校验失败 |
|
|
@ExceptionHandler(MethodArgumentNotValidException.class) |
|
|
@ExceptionHandler(MethodArgumentNotValidException.class) |
|
|
public Result<?> handleBodyValid(MethodArgumentNotValidException ex) { |
|
|
public Result<?> handleBodyValid(MethodArgumentNotValidException ex) { |
|
|