diff --git a/len-common/src/main/java/com/len/handler/BusinessException.java b/len-common/src/main/java/com/len/handler/BusinessException.java new file mode 100644 index 0000000000000000000000000000000000000000..8d2687611e310fd02990a1a8f838529a5a6e144d --- /dev/null +++ b/len-common/src/main/java/com/len/handler/BusinessException.java @@ -0,0 +1,13 @@ +package com.len.handler; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class BusinessException extends RuntimeException { + private Integer code; + private String errMsg; +} diff --git a/len-common/src/main/java/com/len/handler/GlobalExceptionHandler.java b/len-common/src/main/java/com/len/handler/GlobalExceptionHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..265c6894ccf5316cec69ad60a215a71d518fdf74 --- /dev/null +++ b/len-common/src/main/java/com/len/handler/GlobalExceptionHandler.java @@ -0,0 +1,54 @@ +package com.len.handler; + + +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.len.response.Result; +import com.len.response.ResultCode; + +@ControllerAdvice +public class GlobalExceptionHandler { + /** + * 全局异常处理,没有指定异常的类型,不管什么异常都是可以捕获的 + * @param e + * @return + */ + @ExceptionHandler(Exception.class) + @ResponseBody + public Result error(Exception e){ + //e.printStackTrace(); + System.err.println(e.getMessage()); + return Result.error(); + } + + /** + * 处理特定异常类型,可以定义多个,这里只以ArithmeticException为例 + * @param e + * @return + */ + @ExceptionHandler(ArithmeticException.class) + @ResponseBody + public Result error(ArithmeticException e){ + //e.printStackTrace(); + System.err.println(e.getMessage()); + return Result.error().code(ResultCode.ARITHMETIC_EXCEPTION.getCode()) + .message(ResultCode.ARITHMETIC_EXCEPTION.getMessage()); + } + + /** + * 处理业务异常,我们自己定义的异常 + * @param e + * @return + */ + @ExceptionHandler(BusinessException.class) + @ResponseBody + public Result error(BusinessException e){ + //e.printStackTrace(); + System.err.println(e.getErrMsg()); + return Result.error().code(e.getCode()) + .message(e.getErrMsg()); + } + +} diff --git a/len-common/src/main/java/com/len/response/Result.java b/len-common/src/main/java/com/len/response/Result.java new file mode 100644 index 0000000000000000000000000000000000000000..7728ef223706d26628a756970a01e28e69e26489 --- /dev/null +++ b/len-common/src/main/java/com/len/response/Result.java @@ -0,0 +1,86 @@ +package com.len.response; + +import java.util.HashMap; +import java.util.Map; + +import lombok.Data; + +@Data +public class Result { + + private Boolean success; + + private Integer code; + + private String message; + + private T data; + + /** + * 构造方法私有化,里面的方法都是静态方法 + * 达到保护属性的作用 + */ + private Result(){ + + } + + /** + * 这里是使用链式编程 + */ + public static Result ok(){ + Result result = new Result(); + result.setSuccess(true); + result.setCode(ResultCode.SUCCESS.getCode()); + result.setMessage(ResultCode.SUCCESS.getMessage()); + return result; + } + + /** + * 自定义返回成功与否 + * @param success + * @return + */ + public Result success(Boolean success){ + this.setSuccess(success); + return this; + } + + public Result message(String message){ + this.setMessage(message); + return this; + } + + public Result code(Integer code){ + this.setCode(code); + return this; + } + + public static Result success(T data){ + Result result = new Result(); + result.setSuccess(true); + result.setCode(ResultCode.SUCCESS.getCode()); + result.setMessage(ResultCode.SUCCESS.getMessage()); + result.setData(data); + return result; + } + + public static Result error(){ + Result result = new Result(); + result.setSuccess(false); + result.setCode(ResultCode.COMMON_FAIL.getCode()); + result.setMessage(ResultCode.COMMON_FAIL.getMessage()); + return result; + } + + + +// public Result data(String key,Object value){ +// this.data.put(key,value); +// return this; +// } +// +// public Result data(Map map){ +// this.setData(map); +// return this; +// } +} diff --git a/len-common/src/main/java/com/len/response/ResultCode.java b/len-common/src/main/java/com/len/response/ResultCode.java new file mode 100644 index 0000000000000000000000000000000000000000..ea39deab4ca067fe40588960f016911d9abe5939 --- /dev/null +++ b/len-common/src/main/java/com/len/response/ResultCode.java @@ -0,0 +1,109 @@ +package com.len.response; + +public enum ResultCode { + /* 成功 */ + SUCCESS(200, "成功"), + + /* 默认失败 */ + COMMON_FAIL(999, "失败"), + + /* 参数错误:1000~1999 */ + /** + * 参数无效 + */ + PARAM_NOT_VALID(1001, "参数无效"), + /** + * 参数为空 + */ + PARAM_IS_BLANK(1002, "参数为空"), + /** + * 参数类型错误 + */ + PARAM_TYPE_ERROR(1003, "参数类型错误"), + /** + * 参数缺失 + */ + PARAM_NOT_COMPLETE(1004, "参数缺失"), + + /* 用户错误 */ + /** + * 用户未登录 + */ + USER_NOT_LOGIN(2001, "用户未登录"), + /** + * 账号已过期 + */ + USER_ACCOUNT_EXPIRED(2002, "账号已过期"), + /** + * 密码错误 + */ + USER_CREDENTIALS_ERROR(2003, "密码错误"), + /** + * 密码过期 + */ + USER_CREDENTIALS_EXPIRED(2004, "密码过期"), + /** + * 账号不可用 + */ + USER_ACCOUNT_DISABLE(2005, "账号不可用"), + /** + * 账号被锁定 + */ + USER_ACCOUNT_LOCKED(2006, "账号被锁定"), + /** + * 账号不存在 + */ + USER_ACCOUNT_NOT_EXIST(2007, "账号不存在"), + /** + * 账号已存在 + */ + USER_ACCOUNT_ALREADY_EXIST(2008, "账号已存在"), + /** + * 账号下线 + */ + USER_ACCOUNT_USE_BY_OTHERS(2009, "账号下线"), + + /*部门错误*/ + /** + * 部门不存在 + */ + DEPARTMENT_NOT_EXIST(3007, "部门不存在"), + /** + * 部门已存在 + */ + DEPARTMENT_ALREADY_EXIST(3008, "部门已存在"), + + /* 业务错误 */ + /** + * 没有权限 + */ + NO_PERMISSION(3001, "没有权限"), + + /*运行时异常*/ + /** + * 算数异常 + */ + ARITHMETIC_EXCEPTION(9001,"算数异常"), + /** + * 查询无果 + */ + NO_SEARCH(4001,"查询无果"); + + private Integer code; + + private String message; + + ResultCode(Integer code,String message){ + this.code = code; + this.message = message; + } + + public Integer getCode() { + return code; + } + + public String getMessage() { + return message; + } + +} diff --git a/len-common/src/main/java/com/len/util/DataUtil.java b/len-common/src/main/java/com/len/util/DataUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..c4fb8f9bcfc337e7d60a74201e47d33cff655953 --- /dev/null +++ b/len-common/src/main/java/com/len/util/DataUtil.java @@ -0,0 +1,5 @@ +package com.len.util; + +public class DataUtil { + +} diff --git a/pom.xml b/pom.xml index 2fef7af9ac1d9c192f9c95a7eba84012de8ca410..ba6542b21bc231738c7d9aad1cdb30abc673e9f5 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,9 @@ 1.7.25 2.0.1.Final 6.0.10.Final + + 3.0.0 + 1.3.6 @@ -115,7 +118,7 @@ org.mybatis.generator mybatis-generator-maven-plugin - 1.3.6 + ${mybatis-generator-maven-plugin.version} len-web/src/main/resources/auto-config/mybatis-config.xml @@ -127,7 +130,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 3.0.0 + ${maven-javadoc-plugin.version}