Commit 704fedf2 by 丁伟峰

for test

1 parent 2062cc96
package com.alibaba.cloud.integration.order.controller;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import com.alibaba.cloud.integration.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.Objects;
@ControllerAdvice
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
// 处理 BindException 异常
@ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST) // 可选:设置响应状态码
@ResponseBody
public Result<?> handleBindException(BindException ex) {
// 处理异常并返回自定义的错误信息
log.error("======== xxxxxxxxxxx ====> {}", ex.toString());
return Result.failed("请求参数验证失败:" + ex.getMessage());
public Result<?> handleBindException(BindException e) {
// 处理 BindException 异常并返回自定义错误信息
return Result.failed("Invalid request parameters");
}
@ExceptionHandler(Exception.class)
@ResponseBody
public Result<?> handleException(Exception e) {
if (e instanceof MethodArgumentNotValidException) {
log.error("======== MethodArgumentNotValidException ====> {}", e.toString());
FieldError e1 = ((MethodArgumentNotValidException) e).getBindingResult().getFieldError();
return Result.failed(Objects.requireNonNull(e1).getDefaultMessage());
} else {
log.error("====== 其他错误 ===={}", e.toString());
return Result.failed(e.getMessage());
}
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!