Commit 738f0ceb by 丁伟峰

for test

1 parent 4fcb2061
package com.alibaba.cloud.integration.order.controller; package com.alibaba.cloud.integration.order.controller;
import com.alibaba.cloud.integration.common.Result;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError; import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
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.ResponseBody;
import java.util.ArrayList; import java.util.Objects;
import java.util.List;
@ControllerAdvice
@Slf4j @Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler { public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class) @ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleValidationException(MethodArgumentNotValidException ex) { @ResponseBody
List<String> errors = new ArrayList<>(); public Result<?> handleException(Exception e) {
for (FieldError error : ex.getBindingResult().getFieldErrors()) { if (e instanceof MethodArgumentNotValidException) {
errors.add(error.getDefaultMessage()); 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());
} }
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.BAD_REQUEST, "Validation failed", errors);
return ResponseEntity.badRequest().body(errorResponse);
}
@ExceptionHandler(BindException.class)
public ResponseEntity<ErrorResponse> handleBindException(BindException ex) {
log.error("$$$$$$$$$$$$$$$$$$$$ ====> {}", ex.toString());
List<String> errors = new ArrayList<>();
for (ObjectError error : ex.getBindingResult().getAllErrors()) {
errors.add(error.getDefaultMessage());
}
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.BAD_REQUEST, "Binding failed", errors);
return ResponseEntity.badRequest().body(errorResponse);
}
// 其他异常处理方法...
// 定义错误响应类
private static class ErrorResponse {
private final int status;
private final String message;
private final List<String> errors;
public ErrorResponse(HttpStatus status, String message, List<String> errors) {
this.status = status.value();
this.message = message;
this.errors = errors;
}
// 省略 getter 方法...
} }
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!