Browse Source

:white_check_mark: 代码优化

Aron 6 years ago
parent
commit
5edceca113

+ 16 - 31
src/main/java/com/ifast/common/exception/handler/ApplicationExceptionHandler.java

@@ -18,7 +18,6 @@ import org.springframework.web.servlet.NoHandlerFoundException;
 
 /**
  * 异常处理器
- * 
  */
 @RestControllerAdvice
 @Slf4j
@@ -31,9 +30,7 @@ public class ApplicationExceptionHandler {
      */
     @ExceptionHandler(IllegalArgumentException.class)
     public Result<String> illegalArgumentException(IllegalArgumentException e) {
-        if(log.isDebugEnabled()){
-            log.debug("全局异常处理:IllegalArgumentException[{}]:{}",e.getClass(),e.getMessage());
-        }
+        log.error("全局异常处理:IllegalArgumentException[{}]:{}", e.getClass(), e.getMessage());
         return Result.build(EnumErrorCode.illegalArgument.getCode(), e.getMessage());
     }
 
@@ -42,9 +39,7 @@ public class ApplicationExceptionHandler {
      */
     @ExceptionHandler(IFastApiException.class)
     public Result<String> handleIFastAPIException(IFastApiException e) {
-        if(log.isDebugEnabled()){
-            log.debug("全局异常处理:IFastApiException[{}]:{}",e.getClass(),e.getMessage());
-        }
+        log.info("全局异常处理:IFastApiException[{}]:{}", e.getClass(), e.getMessage());
         return getStringResult(e.getMessage());
     }
 
@@ -53,14 +48,12 @@ public class ApplicationExceptionHandler {
      */
     @ExceptionHandler(IFastException.class)
     public Object handleIFastException(IFastException e) {
-        if(log.isDebugEnabled()){
-            log.debug("全局异常处理:IFastException[{}]:{}",e.getClass(),e.getMessage());
-        }
-        if(!HttpContextUtils.isAjax()){
+        log.error("全局异常处理:IFastException[{}]:{}", e.getClass(), e.getMessage());
+        if (!HttpContextUtils.isAjax()) {
             ModelAndView mv = new ModelAndView();
             mv.setViewName(ERROR_DEFAULT_PAGE);
             return mv;
-        }else{
+        } else {
             return getStringResult(e.getMessage());
         }
     }
@@ -78,30 +71,24 @@ public class ApplicationExceptionHandler {
 
     @ExceptionHandler(DuplicateKeyException.class)
     public Result<String> handleDuplicateKeyException(DuplicateKeyException e) {
-        if(log.isDebugEnabled()){
-            log.debug("全局异常处理:DuplicateKeyException[{}]:{}",e.getClass(),e.getMessage());
-        }
+        log.error("全局异常处理:DuplicateKeyException[{}]:{}", e.getClass(), e.getMessage());
         return Result.build(EnumErrorCode.duplicateKeyExist.getCode(), EnumErrorCode.duplicateKeyExist.getMsg());
     }
 
     @ExceptionHandler(NoHandlerFoundException.class)
     public Result<String> noHandlerFoundException(NoHandlerFoundException e) {
-        if(log.isDebugEnabled()){
-            log.debug("全局异常处理:NoHandlerFoundException[{}]:{}",e.getClass(),e.getMessage());
-        }
+        log.error("全局异常处理:NoHandlerFoundException[{}]:{}", e.getClass(), e.getMessage());
         return Result.build(EnumErrorCode.pageNotFound.getCode(), EnumErrorCode.pageNotFound.getMsg());
     }
 
     @ExceptionHandler(ShiroException.class)
     public Result<String> handleAuthorizationException(ShiroException e) {
-        if(log.isDebugEnabled()){
-            log.debug("全局异常处理:ShiroException[{}][{}]:{}",e.getClass(),e.getClass(), e.getMessage());
-        }
-        if(e instanceof IncorrectCredentialsException) {
-        	return Result.build(EnumErrorCode.apiAuthorizationFailed.getCode(), EnumErrorCode.apiAuthorizationFailed.getMsg());
-        }else if(e instanceof ExpiredCredentialsException) {
-        	return Result.build(EnumErrorCode.apiAuthorizationExpired.getCode(), EnumErrorCode.apiAuthorizationExpired.getMsg());
-        } else if(e instanceof AuthenticationException){
+        log.error("全局异常处理:ShiroException[{}][{}]:{}", e.getClass(), e.getClass(), e.getMessage());
+        if (e instanceof IncorrectCredentialsException) {
+            return Result.build(EnumErrorCode.apiAuthorizationFailed.getCode(), EnumErrorCode.apiAuthorizationFailed.getMsg());
+        } else if (e instanceof ExpiredCredentialsException) {
+            return Result.build(EnumErrorCode.apiAuthorizationExpired.getCode(), EnumErrorCode.apiAuthorizationExpired.getMsg());
+        } else if (e instanceof AuthenticationException) {
             return Result.build(EnumErrorCode.userLoginFail.getCode(), EnumErrorCode.userLoginFail.getMsg());
 
         }
@@ -110,14 +97,12 @@ public class ApplicationExceptionHandler {
 
     @ExceptionHandler(Exception.class)
     public Object handleException(Exception e) {
-        if(log.isDebugEnabled()){
-            log.debug("全局异常处理:Exception[{}]:{}",e.getClass(),e.getMessage());
-        }
-        if(!HttpContextUtils.isAjax()){
+        log.error("全局异常处理:Exception[{}]:{}", e.getClass(), e.getMessage());
+        if (!HttpContextUtils.isAjax()) {
             ModelAndView mv = new ModelAndView();
             mv.setViewName(ERROR_DEFAULT_PAGE);
             return mv;
-        }else{
+        } else {
             return Result.build(EnumErrorCode.unknowFail.getCode(), EnumErrorCode.unknowFail.getMsg());
         }
 

+ 21 - 3
src/main/java/com/ifast/common/type/EnumErrorCode.java

@@ -8,12 +8,30 @@ package com.ifast.common.type;
  */
 public enum EnumErrorCode {
 
-
-    ok(200, "请求成功")
+    /**
+     * 请求成功
+     */
+    success(0, "请求成功")
+    /**
+     * 请求失败
+     */
+    ,fail(1, "请求失败")
+    /**
+     * 服务端内部错误,代码发生报错 等未处理的未知错误情况
+     */
     , unknowFail(500, "未知错误")
+    /**
+     * 使用ApiAssert断言抛出
+     */
     , illegalArgument(400, "参数校验异常")
+    /**
+     * 没有操作权限时使用
+     */
     , notAuthorization(401, "未授权")
-    , pageNotFound(404, "页面不存在")
+    /**
+     * 404 资源不存在
+     */
+    , pageNotFound(404, "资源不存在")
 
 
     , duplicateKeyExist(40000, "记录已存在")

+ 1 - 3
src/main/java/com/ifast/common/utils/GenUtils.java

@@ -74,9 +74,7 @@ public class GenUtils {
         List<String> baseColumnNames = Arrays.asList("deleted", "version", "createAt", "createBy", "updateAt", "updateBy");
         for (Map<String, String> column : columns) {
         	columnNames.add(column.get("columnName"));
-        	if(baseColumnNames.contains(column.get("columnName"))) {
-        	    continue;
-            }
+        	if(baseColumnNames.contains(column.get("columnName"))) continue;
         	
             ColumnDO columnDO = new ColumnDO();
             columnDO.setColumnName(column.get("columnName"));

+ 36 - 31
src/main/java/com/ifast/common/utils/Result.java

@@ -1,5 +1,7 @@
 package com.ifast.common.utils;
 
+import com.ifast.common.type.EnumErrorCode;
+
 /**
  * <pre>
  * 
@@ -11,60 +13,63 @@ package com.ifast.common.utils;
  */
 public class Result<T> {
 
-    public final static Integer CODE_SUCCESS = 0;
-    public final static Integer CODE_FAIL = 1;
-    public final static String MSG_SUCCESS = "操作成功";
-    public final static String MSG_FAIL = "操作失败";
+    public final static Integer CODE_SUCCESS = EnumErrorCode.success.getCode();
+    public final static Integer CODE_FAIL = EnumErrorCode.fail.getCode();
+    public final static String MSG_SUCCESS = EnumErrorCode.success.getMsg();
+    public final static String MSG_FAIL = EnumErrorCode.fail.getMsg();
 
-    // 响应业务状态 0 成功, 1失败
+    /**
+     * 响应业务状态 0 成功, 1失败
+     */
     private Integer code;
 
-    // 响应消息
+    /**
+     * 响应消息
+     */
     private String msg;
 
-    // 响应中的数据
+    /**
+     * 响应中的数据
+     */
     private T data;
 
-    public static <T> Result<T> build(Integer status, String msg, T data) {
-        return new Result<T>(status, msg, data);
+    public Result() {}
+
+    public Result(Integer status, String msg, T data) {
+        this.code = status;
+        this.msg = msg;
+        this.data = data;
     }
 
+    public Result(T data) {
+        this.code = 0;
+        this.msg = MSG_SUCCESS;
+        this.data = data;
+    }
+
+    // method
+
     public static <T> Result<T> ok(T data) {
-        return new Result<T>(data);
+        return new Result<>(data);
     }
 
     public static <T> Result<T> ok() {
-        return new Result<T>(CODE_SUCCESS, MSG_SUCCESS, null);
+        return new Result<>(CODE_SUCCESS, MSG_SUCCESS, null);
     }
 
     public static <T> Result<T> fail() {
-        return new Result<T>(CODE_FAIL, MSG_FAIL, null);
-    }
-
-    public Result() {
-
+        return new Result<>(CODE_FAIL, MSG_FAIL, null);
     }
 
     public static <T> Result<T> build(Integer status, String msg) {
-        return new Result<T>(status, msg, null);
-    }
-
-    public static <T> Result<T> getResult(T t) {
-        Result<T> result = new Result<>(t);
-        return result;
+        return build(status, msg, null);
     }
 
-    public Result(Integer status, String msg, T data) {
-        this.code = status;
-        this.msg = msg;
-        this.data = data;
+    public static <T> Result<T> build(Integer status, String msg, T data) {
+        return new Result<>(status, msg, data);
     }
 
-    public Result(T data) {
-        this.code = 0;
-        this.msg = MSG_SUCCESS;
-        this.data = data;
-    }
+    // get set
 
     public Integer getCode() {
         return code;

+ 2 - 5
src/main/java/com/ifast/oss/service/impl/FileServiceImpl.java

@@ -8,7 +8,7 @@ import com.ifast.common.utils.FileType;
 import com.ifast.oss.dao.FileDao;
 import com.ifast.oss.domain.FileDO;
 import com.ifast.oss.service.FileService;
-import org.springframework.beans.factory.annotation.Autowired;
+import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
 
 import java.util.Date;
@@ -20,17 +20,14 @@ import java.util.Date;
  * <small> 2018年3月23日 | Aron</small>
  */
 @Service
+@AllArgsConstructor
 public class FileServiceImpl extends CoreServiceImpl<FileDao, FileDO> implements FileService {
 
-    @Autowired
     private IFastProperties ifastConfig;
-    @Autowired
     private UploadServer uploader;
 
     @Override
     public String upload(byte[] uploadBytes, String fileName) {
-        //处理浏览器文件名获取兼容问题
-
         String url = uploader.upload(uploadBytes, FileNameUtils.getFileName(fileName, ifastConfig));
         FileDO sysFile = new FileDO(FileType.fileType(fileName), url, new Date());
         super.insert(sysFile);