chenwen 1 år sedan
förälder
incheckning
354a5acf8f
2 ändrade filer med 14 tillägg och 2 borttagningar
  1. 1 0
      .classpath
  2. 13 2
      src/main/java/com/hb/proj/allconfig/RequestValidateExceptionHandler.java

+ 1 - 0
.classpath

@@ -26,6 +26,7 @@
 	</classpathentry>
 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
 		<attributes>
+			<attribute name="module" value="true"/>
 			<attribute name="maven.pomderived" value="true"/>
 		</attributes>
 	</classpathentry>

+ 13 - 2
src/main/java/com/hb/proj/allconfig/RequestValidateExceptionHandler.java

@@ -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);
 		
 	}
 }