From 4cef507ed52b5950c2a9620a96bd5f753dd14431 Mon Sep 17 00:00:00 2001 From: zhaoqifeng Date: Thu, 30 Apr 2020 09:35:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E7=BB=9F=E4=B8=80=E8=BF=94=E5=9B=9E7000?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tools/exception/EpmetErrorCode.java | 2 +- .../tools/exception/RenExceptionHandler.java | 16 ++++ .../tools/exception/ValidateException.java | 87 +++++++++++++++++++ .../tools/validator/ValidatorUtils.java | 7 +- .../epmet/dto/form/StaffSubmitFromDTO.java | 1 - 5 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/ValidateException.java diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java index b60c3a40d1..c9b876389c 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java @@ -10,7 +10,7 @@ public enum EpmetErrorCode { ERR10006(10006, "登录超时,请重新登录"), ERR10007(10007, "当前帐号已在别处登录"), ERR401(401, "未授权"), - + VALIDATE_ERROR(7000, "数据校验异常"), SERVER_ERROR(8000, "服务器开小差了..."), CANNOT_JOIN_GROUP(8001, "只有认证党员和居民才可以加入小组,请选择您的身份"), CANNOT_CREATE_GROUP(8002, "只有党员和热心居民才能创建小组,请选择您的身份"), diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/RenExceptionHandler.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/RenExceptionHandler.java index e49e03d52d..b7d8fb03ec 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/RenExceptionHandler.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/RenExceptionHandler.java @@ -61,6 +61,22 @@ public class RenExceptionHandler { // return new Result().error(); } + + /** + * 处理自定义异常 + * "code": 8000, + * "msg": "服务器开小差了...", + */ + @ExceptionHandler(ValidateException.class) + public Result handleVException(ValidateException ex){ + logger.error(ExceptionUtils.getErrorStackTrace(ex)); + Result result=new Result(); + result.setCode(ex.getCode()); + result.setMsg(ex.getMsg()); + return result; +// return new Result().error(); + } + /** * 运行时异常拦截 * "code": 8000, diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/ValidateException.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/ValidateException.java new file mode 100644 index 0000000000..ee165ef2e8 --- /dev/null +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/ValidateException.java @@ -0,0 +1,87 @@ +package com.epmet.commons.tools.exception; + +import com.epmet.commons.tools.utils.MessageUtils; +import org.apache.commons.lang3.StringUtils; + +/** + * @author zhaoqifeng + * @dscription + * @date 2020/4/29 18:13 + */ +public class ValidateException extends RuntimeException { + private static final long serialVersionUID = 1L; + private int code; + private String msg; + + public ValidateException(int code) { + this.code = code; + this.msg = EpmetErrorCode.getMsg(code); + if (StringUtils.isBlank(this.msg)) { + this.msg = MessageUtils.getMessage(code); + } + } + + public ValidateException(int code, String... params) { + this.code = code; + this.msg = EpmetErrorCode.getMsg(code); + if (StringUtils.isBlank(this.msg)) { + this.msg = MessageUtils.getMessage(code, params); + } + } + + public ValidateException(int code, Throwable e) { + super(e); + this.code = code; + this.msg = EpmetErrorCode.getMsg(code); + if (StringUtils.isBlank(this.msg)) { + this.msg = MessageUtils.getMessage(code); + } + } + + public ValidateException(int code, Throwable e, String... params) { + super(e); + this.code = code; + + } + + public ValidateException(int code, String msg) { + this.code = code; + if (StringUtils.isBlank(msg)) { + this.msg = EpmetErrorCode.getMsg(code); + if (StringUtils.isBlank(this.msg)) { + this.msg = MessageUtils.getMessage(code, msg); + } + } else { + this.msg = msg; + } + } + + + public ValidateException(String msg) { + super(msg); + this.code = EpmetErrorCode.VALIDATE_ERROR.getCode(); + this.msg = msg; + } + + public ValidateException(String msg, Throwable e) { + super(msg, e); + this.code = EpmetErrorCode.VALIDATE_ERROR.getCode(); + this.msg = msg; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } +} diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/validator/ValidatorUtils.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/validator/ValidatorUtils.java index 5af37cbdb2..ac14e38ce6 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/validator/ValidatorUtils.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/validator/ValidatorUtils.java @@ -9,6 +9,7 @@ package com.epmet.commons.tools.validator; import com.epmet.commons.tools.exception.RenException; +import com.epmet.commons.tools.exception.ValidateException; import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.support.ResourceBundleMessageSource; @@ -40,10 +41,10 @@ public class ValidatorUtils { * 校验对象 * @param object 待校验对象 * @param groups 待校验的组 - * @throws RenException 校验不通过,则报RenException异常 + * @throws ValidateException 校验不通过,则报RenException异常 */ public static void validateEntity(Object object, Class... groups) - throws RenException { + throws ValidateException { Locale.setDefault(LocaleContextHolder.getLocale()); Validator validator = Validation.byDefaultProvider().configure().messageInterpolator( new ResourceBundleMessageInterpolator(new MessageSourceResourceBundleLocator(getMessageSource()))) @@ -51,7 +52,7 @@ public class ValidatorUtils { Set> constraintViolations = validator.validate(object, groups); if (!constraintViolations.isEmpty()) { ConstraintViolation constraint = constraintViolations.iterator().next(); - throw new RenException(constraint.getMessage()); + throw new ValidateException(constraint.getMessage()); } } } diff --git a/epmet-module/gov-org/gov-org-client/src/main/java/com/epmet/dto/form/StaffSubmitFromDTO.java b/epmet-module/gov-org/gov-org-client/src/main/java/com/epmet/dto/form/StaffSubmitFromDTO.java index 107cff7298..7356f97392 100644 --- a/epmet-module/gov-org/gov-org-client/src/main/java/com/epmet/dto/form/StaffSubmitFromDTO.java +++ b/epmet-module/gov-org/gov-org-client/src/main/java/com/epmet/dto/form/StaffSubmitFromDTO.java @@ -26,7 +26,6 @@ public class StaffSubmitFromDTO implements Serializable { /** * 机关ID */ - @NotBlank(message = "机关ID不能为空") private String agencyId; /** * 人员ID From 3a5781d360f12bcd808810bd86d0b232fc1c897b Mon Sep 17 00:00:00 2001 From: yinzuomei <576302893@qq.com> Date: Thu, 30 Apr 2020 10:00:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=A0=E9=99=A4test=E6=96=B9=E6=B3=95gro?= =?UTF-8?q?up/test/{test}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../epmet/modules/group/controller/ResiGroupController.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/group/controller/ResiGroupController.java b/epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/group/controller/ResiGroupController.java index 84f5c4ad95..67549c75fe 100644 --- a/epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/group/controller/ResiGroupController.java +++ b/epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/group/controller/ResiGroupController.java @@ -47,12 +47,6 @@ public class ResiGroupController { @Autowired private ResiGroupService resiGroupService; - @GetMapping("test/{test}") - public Result test(@PathVariable("test") String test) { - int result = 1 / 0; - return new Result().ok("请求成功啦❤"); - } - /** * @param tokenDto * @param myGroupFormDTO