Commit a5b007ca by 丁伟峰

for test

1 parent 9ec5955d
......@@ -2,6 +2,7 @@ package com.alibaba.cloud.integration.order.controller;
import com.alibaba.cloud.integration.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
......@@ -23,26 +24,26 @@ public class GlobalExceptionHandler {
@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);
List<String> collect = fieldErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.toList());
return Result.instance(400, 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);
List<String> collect = fieldErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.toList());
return Result.instance(400, 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);
List<String> collect = constraintViolations.stream().map(ConstraintViolation::getMessage).collect(Collectors.toList());
return Result.instance(400, BAD_REQUEST_MSG, collect);
}
// 处理以上处理不了的其他异常
@ExceptionHandler(Exception.class)
public Result<?> exceptionHandler(Exception e) {
return Result.instance(HttpStatus.BAD_REQUEST.value(), BAD_REQUEST_MSG, e.getMessage());
return Result.instance(400, 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!