添加参数校验异常处理

This commit is contained in:
udf_admin@163.com
2017-11-09 23:33:15 +08:00
parent 4217dc2849
commit 8062f23804
@@ -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);