Forráskód Böngészése

添加统一处理异常

minitiger 9 éve
szülő
commit
5e98f0c916

+ 31 - 0
VisualInspection_server/src/main/java/com/xintong/visualinspection/aop/ControllerValidatorInterceptor.java

@@ -0,0 +1,31 @@
+package com.xintong.visualinspection.aop;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.springframework.stereotype.Component;
+import org.springframework.validation.BindingResult;
+import org.springframework.validation.ObjectError;
+
+import com.xintong.visualinspection.err.BusinessException;
+@Aspect
+@Component
+public class ControllerValidatorInterceptor {
+	 @Around("execution(* com.xintong.visualinspection.controller..*.*(..)) && args(..,bindingResult)")
+     public Object doAround(ProceedingJoinPoint pjp, BindingResult bindingResult) throws Throwable {
+         Object retVal;
+         if (bindingResult.hasErrors()) {
+        	 
+        	 StringBuffer sb = new StringBuffer();
+        	 for(ObjectError err:bindingResult.getAllErrors()){
+        		 sb.append(err.getDefaultMessage()+";");
+        	 }
+        	 String s = sb.toString();
+        	 if(s.length()>0) s = sb.substring(0, s.length()-1);
+             throw new BusinessException(20001,s);
+         } else {
+             retVal = pjp.proceed();
+         }
+         return retVal;
+     }
+}

+ 34 - 0
VisualInspection_server/src/main/java/com/xintong/visualinspection/err/BusinessException.java

@@ -0,0 +1,34 @@
+package com.xintong.visualinspection.err;
+import lombok.Data;
+@Data
+public class BusinessException extends RuntimeException {
+	
+	/**
+	 * serialVersionUID:TODO(用一句话描述这个变量表示什么)
+	 *
+	 * @since 1.0.0
+	 */
+	
+	private static final long serialVersionUID = 1L;
+	private ErrorCode errCode;
+	
+	public BusinessException() {
+		super();
+		this.setErrCode(new ErrorCode());
+	}
+
+	public BusinessException(int errorCode) {
+		super();
+		this.setErrCode(new ErrorCode(errorCode));
+	}
+	
+	public BusinessException(int errorCode, String msg) {
+		super();
+		this.setErrCode(new ErrorCode(errorCode, msg));
+	}
+
+	public BusinessException(int errorCode, Throwable cause) {
+		super(cause);
+		this.setErrCode(new ErrorCode(errorCode));
+	}
+}

+ 19 - 0
VisualInspection_server/src/main/java/com/xintong/visualinspection/err/ErrorCode.java

@@ -0,0 +1,19 @@
+package com.xintong.visualinspection.err;
+import lombok.Data;
+@Data
+public class ErrorCode {
+	private int code;
+    private String desc;
+
+    public ErrorCode(){}
+    
+    public ErrorCode(int code) {
+        this.setCode(code);
+        this.setDesc(ErrorCodeEnum.getPropByKey(code+""));
+    }
+    
+    public ErrorCode(int code, String desc) {
+        this.setCode(code);
+        this.setDesc(ErrorCodeEnum.getPropByKey(code+"")+ ":" + desc);
+    }
+}

+ 38 - 0
VisualInspection_server/src/main/java/com/xintong/visualinspection/err/ErrorCodeEnum.java

@@ -0,0 +1,38 @@
+package com.xintong.visualinspection.err;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.ResourceBundle;
+import java.util.Set;
+
+public class ErrorCodeEnum {
+	
+	private static final ResourceBundle resourceBundle = ResourceBundle.getBundle("errcode");
+    
+//	private static Map<String,String> errorCodeMap = new HashMap<String,String>();
+//	
+//	static{
+//		setAllErrorCode();
+//	}
+	/**
+     * 获取属性值
+     * 
+     * @return
+     */
+    public static String getPropByKey(String key)
+    {
+    	resourceBundle.keySet();
+        return resourceBundle.getString(key);
+    }
+    
+//    public static Map<String,String> setAllErrorCode()
+//    {
+//    	Set<String> keySet= resourceBundle.keySet();
+//    	for(String key:keySet){
+//    		errorCodeMap.put(key, getPropByKey(key));
+//    	}
+//    	return errorCodeMap;
+//    }
+    
+}

+ 12 - 0
VisualInspection_server/src/main/resources/errcode.properties

@@ -0,0 +1,12 @@
+#\u901a\u7528\u9519\u8bef
+10001=\u7a7a\u6307\u9488\u9519\u8bef
+10002=\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u9519\u8bef
+10003=\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\u9519\u8bef
+10004=\u6570\u5b66\u8fd0\u7b97\u9519\u8bef
+10005=\u6587\u4ef6\u672a\u627e\u5230
+10006=\u53c2\u6570\u9519\u8bef
+11000=\u5176\u4ed6\u9519\u8bef
+#\u901a\u7528\u4e1a\u52a1\u9519\u8bef
+20001=\u8f93\u5165\u53c2\u6570\u9519\u8bef
+
+