Browse Source

数据校验异常统一返回7000

dev_shibei_match
zhaoqifeng 5 years ago
parent
commit
4cef507ed5
  1. 2
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java
  2. 16
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/RenExceptionHandler.java
  3. 87
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/ValidateException.java
  4. 7
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/validator/ValidatorUtils.java
  5. 1
      epmet-module/gov-org/gov-org-client/src/main/java/com/epmet/dto/form/StaffSubmitFromDTO.java

2
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, "只有党员和热心居民才能创建小组,请选择您的身份"),

16
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,

87
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;
}
}

7
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<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
if (!constraintViolations.isEmpty()) {
ConstraintViolation<Object> constraint = constraintViolations.iterator().next();
throw new RenException(constraint.getMessage());
throw new ValidateException(constraint.getMessage());
}
}
}

1
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

Loading…
Cancel
Save