Commit b92bc190 by 丁伟峰

Merge branch 'feat-dingwf-mvp616' into develop

# Conflicts:
#	project-order/src/main/java/com/alibaba/cloud/integration/order/controller/GlobalExceptionHandler.java
2 parents b96f6df5 146ded71
package com.alibaba.cloud.integration.order.controller;
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.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorResponse> handleValidationException(MethodArgumentNotValidException ex) {
List<String> errors = new ArrayList<>();
for (FieldError error : ex.getBindingResult().getFieldErrors()) {
errors.add(error.getDefaultMessage());
}
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.BAD_REQUEST, "Validation failed", errors);
return ResponseEntity.badRequest().body(errorResponse);
}
@ExceptionHandler(BindException.class)
public ResponseEntity<ErrorResponse> handleBindException(BindException ex) {
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!