From 8062f23804ba49fe58ab5eeef2cd6adc43925c41 Mon Sep 17 00:00:00 2001 From: "udf_admin@163.com" Date: Thu, 9 Nov 2017 23:33:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=82=E6=95=B0=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../itkk/udf/core/exception/handle/ExceptionHandle.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/udf-starter-core/src/main/java/org/itkk/udf/core/exception/handle/ExceptionHandle.java b/udf-starter-core/src/main/java/org/itkk/udf/core/exception/handle/ExceptionHandle.java index 10764f1..0adcbf7 100644 --- a/udf-starter-core/src/main/java/org/itkk/udf/core/exception/handle/ExceptionHandle.java +++ b/udf-starter-core/src/main/java/org/itkk/udf/core/exception/handle/ExceptionHandle.java @@ -1,5 +1,6 @@ package org.itkk.udf.core.exception.handle; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -11,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.client.RestClientResponseException; @@ -84,6 +86,13 @@ public class ExceptionHandle extends ResponseEntityExceptionHandler { } catch (IOException e) { throw new SystemRuntimeException(e); } + } else if (ex instanceof MethodArgumentNotValidException) { //参数校验异常 + MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) ex; + try { + errorResult.setMessage(objectMapper.writeValueAsString(methodArgumentNotValidException.getBindingResult().getAllErrors())); + } catch (JsonProcessingException e) { + throw new SystemRuntimeException(e); + } } log.error(ex.getClass().getName(), ex); return super.handleExceptionInternal(ex, new RestResponse<>(localHttpStatus, errorResult), headers, localHttpStatus, request);