From f983d21ebdbbb7f3ce542ca5993659de7f15e8d0 Mon Sep 17 00:00:00 2001 From: xiangwbs Date: Sun, 26 Nov 2017 21:13:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=85=A8=E5=B1=80=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E2=80=94=E8=A1=A8=E5=8D=95=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/GlobalExceptionHandler.java | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/boot-web/src/main/java/com/xwbing/handler/GlobalExceptionHandler.java b/boot-web/src/main/java/com/xwbing/handler/GlobalExceptionHandler.java index bf584d8..2b3100f 100644 --- a/boot-web/src/main/java/com/xwbing/handler/GlobalExceptionHandler.java +++ b/boot-web/src/main/java/com/xwbing/handler/GlobalExceptionHandler.java @@ -5,6 +5,7 @@ import com.xwbing.exception.BusinessException; import com.xwbing.util.JSONObjResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.http.HttpStatus; import org.springframework.validation.BindException; import org.springframework.validation.BindingResult; @@ -18,6 +19,7 @@ import org.springframework.web.bind.annotation.ResponseStatus; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.List; +import java.util.stream.Collectors; /** * 说明: 全局异常处理 @@ -58,15 +60,10 @@ public class GlobalExceptionHandler { @ResponseBody public JSONObject handlerBindException(HttpServletRequest request, HttpServletResponse response, BindException ex) { List list = ex.getAllErrors(); - StringBuilder stringBuffer = new StringBuilder(); - for (ObjectError objectError : list) { - if (stringBuffer.length() > 0) - stringBuffer.append(" && "); - stringBuffer.append(objectError.getDefaultMessage()); - } - logger.error(stringBuffer.toString()); + String errorMessages = list.stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining("&&")); + logger.error(errorMessages); response.setStatus(HttpStatus.OK.value()); - return JSONObjResult.toJSONObj(stringBuffer.toString()); + return JSONObjResult.toJSONObj(errorMessages); } /** @@ -81,18 +78,14 @@ public class GlobalExceptionHandler { public JSONObject handlerMethodArgumentNotValidException(MethodArgumentNotValidException ex) { BindingResult bindingResult = ex.getBindingResult(); List allErrors = bindingResult.getAllErrors(); - StringBuilder stringBuffer = new StringBuilder(); - for (ObjectError objectError : allErrors) { - if (stringBuffer.length() > 0) - stringBuffer.append(" && "); - stringBuffer.append(objectError.getDefaultMessage()); - } - logger.error(stringBuffer.toString()); - return JSONObjResult.toJSONObj(stringBuffer.toString()); + String errorMessages = allErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining("&&")); + logger.error(errorMessages); + return JSONObjResult.toJSONObj(errorMessages); } /** * 全部捕获 + * * @param ex * @return */