Commit a5b007ca by 丁伟峰

for test

1 parent 9ec5955d
...@@ -2,6 +2,7 @@ package com.alibaba.cloud.integration.order.controller; ...@@ -2,6 +2,7 @@ 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.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
import org.springframework.validation.FieldError; import org.springframework.validation.FieldError;
...@@ -23,26 +24,26 @@ public class GlobalExceptionHandler { ...@@ -23,26 +24,26 @@ public class GlobalExceptionHandler {
@ExceptionHandler(BindException.class) @ExceptionHandler(BindException.class)
public Result<?> bindExceptionHandler(BindException e) { public Result<?> bindExceptionHandler(BindException e) {
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors(); List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
List<String> collect = fieldErrors.stream().map(o -> o.getDefaultMessage()).collect(Collectors.toList()); List<String> collect = fieldErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.toList());
return Result.instance(HttpStatus.BAD_REQUEST.value(), BAD_REQUEST_MSG, collect); return Result.instance(400, BAD_REQUEST_MSG, collect);
} }
// 处理 json 请求体调用接口校验失败抛出的异常 // 处理 json 请求体调用接口校验失败抛出的异常
@ExceptionHandler(MethodArgumentNotValidException.class) @ExceptionHandler(MethodArgumentNotValidException.class)
public Result<?> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) { public Result<?> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors(); List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
List<String> collect = fieldErrors.stream().map(o -> o.getDefaultMessage()).collect(Collectors.toList()); List<String> collect = fieldErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.toList());
return Result.instance(HttpStatus.BAD_REQUEST.value(), BAD_REQUEST_MSG, collect); return Result.instance(400, BAD_REQUEST_MSG, collect);
} }
// 处理单个参数校验失败抛出的异常 // 处理单个参数校验失败抛出的异常
@ExceptionHandler(ConstraintViolationException.class) @ExceptionHandler(ConstraintViolationException.class)
public Result<?> constraintViolationExceptionHandler(ConstraintViolationException e) { public Result<?> constraintViolationExceptionHandler(ConstraintViolationException e) {
Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations(); Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
List<String> collect = constraintViolations.stream().map(o -> o.getMessage()).collect(Collectors.toList()); List<String> collect = constraintViolations.stream().map(ConstraintViolation::getMessage).collect(Collectors.toList());
return Result.instance(HttpStatus.BAD_REQUEST.value(), BAD_REQUEST_MSG, collect); return Result.instance(400, BAD_REQUEST_MSG, collect);
} }
// 处理以上处理不了的其他异常 // 处理以上处理不了的其他异常
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public Result<?> exceptionHandler(Exception e) { 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!