yonge 5 years ago
parent
commit
8c142a9e47

+ 9 - 3
mec-common/common-core/src/main/java/com/ym/mec/common/config/EnumConverterConfig.java

@@ -14,9 +14,15 @@ public class EnumConverterConfig implements Converter<String, BaseEnum<?, ?>> {
 	}
 
 	private <T extends BaseEnum<?, ?>> T getEnum(Class<T> targerType, String source) {
-		for (T enumObj : targerType.getEnumConstants()) {
-			if (source.equals(String.valueOf(enumObj.getCode()))) {
-				return enumObj;
+		for (T en : targerType.getEnumConstants()) {
+			if (en.getCode() instanceof Integer) {
+				if ((int) en.getCode() == Integer.parseInt(source.toString())) {
+					return en;
+				}
+			} else {
+				if (source.equals(String.valueOf(en.getCode()))) {
+					return en;
+				}
 			}
 		}
 		return null;

+ 40 - 11
mec-common/common-core/src/main/java/com/ym/mec/common/controller/BaseController.java

@@ -1,28 +1,39 @@
 package com.ym.mec.common.controller;
 
-import com.ym.mec.common.entity.HttpCode;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+
 import com.ym.mec.common.entity.HttpResponseResult;
 
+@ControllerAdvice
 public class BaseController {
+	
+	private final static Logger logger = LoggerFactory.getLogger(BaseController.class);
 
 	public static HttpResponseResult succeed(Object object) {
-		return getResponseData(true, HttpCode.DEFAULT_SUCCESS_CODE, object, "");
+		return getResponseData(true, HttpStatus.OK, object, "");
 	}
 
 	public static HttpResponseResult succeed() {
-		return getResponseData(true, HttpCode.DEFAULT_SUCCESS_CODE, null, "");
+		return getResponseData(true, HttpStatus.OK, null, "");
 	}
 
 	public static HttpResponseResult succeed(String message) {
-		return failed(HttpCode.DEFAULT_SUCCESS_CODE, message);
+		return failed(HttpStatus.OK, message);
 	}
 
 	public static HttpResponseResult succeedData(Object obj) {
-		return getResponseData(true, HttpCode.DEFAULT_SUCCESS_CODE, obj, "操作成功");
+		return getResponseData(true, HttpStatus.OK, obj, "操作成功");
 	}
 
 	public static HttpResponseResult warned(String message) {
-		return failed(HttpCode.PART_THROW, message);
+		return failed(HttpStatus.MULTI_STATUS, message);
 	}
 
 	public static HttpResponseResult failed() {
@@ -30,20 +41,38 @@ public class BaseController {
 	}
 
 	public static HttpResponseResult failed(String msg) {
-		return failed(HttpCode.DEFAULT_FAILD_CODE, msg);
+		return failed(HttpStatus.INTERNAL_SERVER_ERROR, msg);
 	}
 
-	public static HttpResponseResult failed(int code, String msg) {
-		return getResponseData(false, code, null, msg);
+	public static HttpResponseResult failed(HttpStatus statusCode, String msg) {
+		return getResponseData(false, statusCode, null, msg);
 	}
 
-	private static HttpResponseResult getResponseData(boolean status, int code, Object data, String message) {
+	private static HttpResponseResult getResponseData(boolean status, HttpStatus statusCode, Object data, String message) {
 		HttpResponseResult obj = new HttpResponseResult();
 		obj.setStatus(status);
-		obj.setCode(code);
+		obj.setCode(statusCode.value());
 		obj.setData(data);
 		obj.setMsg(message);
 		return obj;
 	}
+	
+	/**
+	 * 处理一般异常
+	 * 
+	 * @param ex
+	 * @param request
+	 * @return
+	 */
+	@ExceptionHandler(Exception.class)
+	public HttpResponseResult handleException(Exception ex, HttpServletRequest request) {
+		Throwable e = ExceptionUtils.getRootCause(ex);
+		if (e == null) {
+			e = ex;
+		}
+
+		logger.error("System Error", e);
+		return failed(e.getMessage());
+	}
 
 }

+ 0 - 22
mec-common/common-core/src/main/java/com/ym/mec/common/entity/HttpCode.java

@@ -1,22 +0,0 @@
-package com.ym.mec.common.entity;
-
-/**
- * http状态码
- */
-public class HttpCode {
-
-	/** 未登陆 */
-	public static final int NO_LOGIN = 401;
-
-	/** 无权限 */
-	public static final int NO_PERM = 403;
-
-	/** 部分成功 */
-	public static final int PART_THROW = 206;
-
-	/** 成功 */
-	public static final int DEFAULT_SUCCESS_CODE = 200;
-
-	/** 失败 */
-	public final static int DEFAULT_FAILD_CODE = 300;
-}

+ 0 - 46
mec-common/common-core/src/main/java/com/ym/mec/common/exception/BasicControllerAdvice.java

@@ -1,46 +0,0 @@
-package com.ym.mec.common.exception;
-
-import javax.servlet.http.HttpServletRequest;
-
-import org.apache.commons.lang3.exception.ExceptionUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.http.HttpStatus;
-import org.springframework.security.access.AccessDeniedException;
-import org.springframework.web.bind.annotation.ControllerAdvice;
-import org.springframework.web.bind.annotation.ExceptionHandler;
-import org.springframework.web.bind.annotation.ResponseBody;
-
-import com.ym.mec.common.controller.BaseController;
-import com.ym.mec.common.entity.HttpResponseResult;
-
-@ControllerAdvice
-public class BasicControllerAdvice extends BaseController {
-
-	private final static Logger logger = LoggerFactory.getLogger(BasicControllerAdvice.class);
-
-	/**
-	 * 处理一般异常
-	 * 
-	 * @param ex
-	 * @param request
-	 * @return
-	 */
-	@ExceptionHandler(Exception.class)
-	@ResponseBody
-	public HttpResponseResult handleException(Exception ex, HttpServletRequest request) {
-		Throwable e = ExceptionUtils.getRootCause(ex);
-		if (e == null) {
-			e = ex;
-		}
-
-		if (e instanceof AccessDeniedException) {
-			logger.error("Access Denied", e);
-			return failed(HttpStatus.FORBIDDEN.value(), "禁止访问");
-		}
-
-		logger.error("System Error", e);
-		return failed(e.getMessage());
-	}
-
-}