mirror of
https://gitee.com/coder-xiaomo/flashsale
synced 2025-09-13 23:41:39 +08:00
优化校验规则
This commit is contained in:
@@ -8,6 +8,8 @@ import com.cxyxiaomo.flashsale.error.BusinessException;
|
||||
import com.cxyxiaomo.flashsale.error.EmBusinessError;
|
||||
import com.cxyxiaomo.flashsale.service.UserService;
|
||||
import com.cxyxiaomo.flashsale.service.model.UserModel;
|
||||
import com.cxyxiaomo.flashsale.validator.ValidationResult;
|
||||
import com.cxyxiaomo.flashsale.validator.ValidatorImpl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -20,9 +22,13 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
@Autowired
|
||||
private UserDOMapper userDOMapper;
|
||||
|
||||
@Autowired
|
||||
private UserPasswordDOMapper userPasswordDOMapper;
|
||||
|
||||
@Autowired
|
||||
private ValidatorImpl validator;
|
||||
|
||||
@Override
|
||||
public UserModel getUserById(Integer id) {
|
||||
// 调用UserDataObjectMapper获取到对应的用户DataObject
|
||||
@@ -60,11 +66,11 @@ public class UserServiceImpl implements UserService {
|
||||
if (userModel == null) {
|
||||
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
|
||||
}
|
||||
if (StringUtils.isEmpty(userModel.getName())
|
||||
|| userModel.getGender() == null
|
||||
|| userModel.getAge() == null
|
||||
|| StringUtils.isEmpty(userModel.getTelephone())) {
|
||||
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
|
||||
|
||||
// (优化校验规则)
|
||||
ValidationResult result = validator.validate(userModel);
|
||||
if (result.isHasErrors()) {
|
||||
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, result.getErrMsg());
|
||||
}
|
||||
|
||||
// 实现 model -> dataobject 方法
|
||||
|
@@ -1,14 +1,33 @@
|
||||
package com.cxyxiaomo.flashsale.service.model;
|
||||
|
||||
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
public class UserModel {
|
||||
private Integer id;
|
||||
|
||||
@NotBlank(message = "用户名不能为空")
|
||||
private String name;
|
||||
|
||||
@NotNull(message = "姓名不能不填写")
|
||||
private Byte gender;
|
||||
|
||||
@NotNull(message = "年龄不能不填写")
|
||||
@Min(value = 0, message = "年龄必须大于0")
|
||||
@Max(value = 150, message = "年龄必须小于150")
|
||||
private Integer age;
|
||||
|
||||
@NotBlank(message = "手机号不能为空")
|
||||
private String telephone;
|
||||
|
||||
private String registerMode;
|
||||
|
||||
private String thirdPartyId;
|
||||
|
||||
@NotBlank(message = "密码不能为空")
|
||||
private String encryptPassword;
|
||||
|
||||
public Integer getId() {
|
||||
|
@@ -0,0 +1,35 @@
|
||||
package com.cxyxiaomo.flashsale.validator;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ValidationResult {
|
||||
// 校验结果是否有错
|
||||
private boolean hasErrors = false;
|
||||
|
||||
// 存放错误信息的Map
|
||||
private Map<String, String> errMsgMap = new HashMap<>();
|
||||
|
||||
// 实现通用的通过格式化字符串信息获取错误结果的msg方法
|
||||
public String getErrMsg() {
|
||||
return StringUtils.join(errMsgMap.values().toArray(), ",");
|
||||
}
|
||||
|
||||
public boolean isHasErrors() {
|
||||
return hasErrors;
|
||||
}
|
||||
|
||||
public void setHasErrors(boolean hasErrors) {
|
||||
this.hasErrors = hasErrors;
|
||||
}
|
||||
|
||||
public Map<String, String> getErrMsgMap() {
|
||||
return errMsgMap;
|
||||
}
|
||||
|
||||
public void setErrMsgMap(Map<String, String> errMsgMap) {
|
||||
this.errMsgMap = errMsgMap;
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
package com.cxyxiaomo.flashsale.validator;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import java.util.Set;
|
||||
|
||||
@Component
|
||||
public class ValidatorImpl implements InitializingBean {
|
||||
|
||||
private Validator validator;
|
||||
|
||||
// 实现校验方法并返回校验结果
|
||||
public ValidationResult validate(Object bean) {
|
||||
ValidationResult result = new ValidationResult();
|
||||
Set<ConstraintViolation<Object>> constraintViolationSet = validator.validate(bean);
|
||||
if (constraintViolationSet.size() > 0) {
|
||||
// 有错误
|
||||
result.setHasErrors(true);
|
||||
constraintViolationSet.forEach(constraintViolation -> {
|
||||
String errMsg = constraintViolation.getMessage();
|
||||
String propertyName = constraintViolation.getPropertyPath().toString();
|
||||
result.getErrMsgMap().put(propertyName, errMsg);
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
// 将Hibernate Validator通过工厂的初始化方式使其实例化
|
||||
this.validator = (Validator) Validation.buildDefaultValidatorFactory().getValidator();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user