Commit 9ec5955d by 丁伟峰

for test

1 parent 02ac9b99
...@@ -2,54 +2,47 @@ package com.alibaba.cloud.integration.order.controller; ...@@ -2,54 +2,47 @@ package com.alibaba.cloud.integration.order.controller;
import com.alibaba.cloud.integration.common.Result; 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.validation.BindException; import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError; import org.springframework.validation.FieldError;
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;
import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException; import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException; import java.util.List;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Slf4j @Slf4j
@RestControllerAdvice @RestControllerAdvice
public class GlobalExceptionHandler { public class GlobalExceptionHandler {
private static final String BAD_REQUEST_MSG = "参数检验不通过";
@ExceptionHandler(value = {BindException.class, ValidationException.class, MethodArgumentNotValidException.class, Exception.class}) //处理 form data方式调用接口校验失败抛出的异常
public Result<?> handleValidatedException(Exception e) { @ExceptionHandler(BindException.class)
Result<?> resp = null; public Result<?> bindExceptionHandler(BindException e) {
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
log.error("=======$$$$$$$$$$$$======" + e.toString()); List<String> collect = fieldErrors.stream().map(o -> o.getDefaultMessage()).collect(Collectors.toList());
return Result.instance(HttpStatus.BAD_REQUEST.value(), BAD_REQUEST_MSG, collect);
if (e instanceof MethodArgumentNotValidException) { }
// BeanValidation exception // 处理 json 请求体调用接口校验失败抛出的异常
MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e; @ExceptionHandler(MethodArgumentNotValidException.class)
resp = Result.failed( public Result<?> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
ex.getBindingResult().getAllErrors().stream() List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
.map(ObjectError::getDefaultMessage) List<String> collect = fieldErrors.stream().map(o -> o.getDefaultMessage()).collect(Collectors.toList());
.collect(Collectors.joining("; ")) return Result.instance(HttpStatus.BAD_REQUEST.value(), BAD_REQUEST_MSG, collect);
); }
} else if (e instanceof ConstraintViolationException) { // 处理单个参数校验失败抛出的异常
// BeanValidation GET simple param @ExceptionHandler(ConstraintViolationException.class)
ConstraintViolationException ex = (ConstraintViolationException) e; public Result<?> constraintViolationExceptionHandler(ConstraintViolationException e) {
resp = Result.failed( Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
ex.getConstraintViolations().stream() List<String> collect = constraintViolations.stream().map(o -> o.getMessage()).collect(Collectors.toList());
.map(ConstraintViolation::getMessage) return Result.instance(HttpStatus.BAD_REQUEST.value(), BAD_REQUEST_MSG, collect);
.collect(Collectors.joining("; ")) }
); // 处理以上处理不了的其他异常
} else if (e instanceof BindException) { @ExceptionHandler(Exception.class)
// BeanValidation GET object param public Result<?> exceptionHandler(Exception e) {
BindException ex = (BindException) e; return Result.instance(HttpStatus.BAD_REQUEST.value(), BAD_REQUEST_MSG, e.getMessage());
resp = Result.failed(ex.getAllErrors().stream()
.map(ObjectError::getDefaultMessage)
.collect(Collectors.joining("; "))
);
} else {
resp = Result.failed("其他错误信息,这个逻辑应该不会走到 ~~~ ");
}
return resp;
} }
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!