Commit 46247ac3 by 丁伟峰

Merge branch 'feat-dingwf-mvp616' into develop

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