|
@@ -11,6 +11,7 @@ import org.springframework.web.HttpRequestMethodNotSupportedException;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
|
|
+import com.fasterxml.jackson.databind.exc.InvalidFormatException;
|
|
|
import com.hb.proj.utils.RespVO;
|
|
|
import com.hb.proj.utils.RespVOBuilder;
|
|
|
|
|
@@ -22,6 +23,7 @@ public class RequestValidateExceptionHandler {
|
|
|
|
|
|
private static final Logger logger=LoggerFactory.getLogger(RequestValidateExceptionHandler.class);
|
|
|
|
|
|
+
|
|
|
//处理请求参数格式错误 @RequestParam上validate失败后抛出的异常是javax.validation.ConstraintViolationException
|
|
|
@ExceptionHandler(ConstraintViolationException.class)
|
|
|
public RespVO<Object> constraintViolationExceptionHandler(ConstraintViolationException e) {
|
|
@@ -56,9 +58,18 @@ public class RequestValidateExceptionHandler {
|
|
|
|
|
|
@ExceptionHandler(Exception.class)
|
|
|
public RespVO<Object> otherExceptionHandler(Exception e) {
|
|
|
- boolean isBE=e instanceof BusinessException;
|
|
|
logger.error("服务出错",e);
|
|
|
- return RespVOBuilder.error(RespVOBuilder.API_EXE_ERROR,isBE?e.getMessage():"服务出错");
|
|
|
+
|
|
|
+
|
|
|
+ String msg="服务出错";
|
|
|
+ if(e.getCause() instanceof InvalidFormatException) {
|
|
|
+ msg="数据格式错误";
|
|
|
+ }
|
|
|
+ else if(e instanceof BusinessException) {
|
|
|
+ msg=e.getMessage();
|
|
|
+ }
|
|
|
+
|
|
|
+ return RespVOBuilder.error(RespVOBuilder.API_EXE_ERROR,msg);
|
|
|
|
|
|
}
|
|
|
}
|