mirror of
https://gitee.com/coder-xiaomo/flashsale
synced 2025-09-11 06:21:40 +08:00
优化校验规则
This commit is contained in:
@@ -15,7 +15,7 @@
|
|||||||
### 层次结构(以User为例,自上向下)
|
### 层次结构(以User为例,自上向下)
|
||||||
|
|
||||||
| | 目录 | Java对象类型 | 说明 | 举例 |
|
| | 目录 | Java对象类型 | 说明 | 举例 |
|
||||||
|-------------------|-----------------------|---------------------------------|-------------------------------|------------------------------|
|
| ----------------- | --------------------- | --------------------------------------- | ---------------------------------------- | ------------------------------- |
|
||||||
| **Controller层** | | | | |
|
| **Controller层** | | | | |
|
||||||
| Controller | controller | 类 class | | controller.UserController |
|
| Controller | controller | 类 class | | controller.UserController |
|
||||||
| View Object | controller/viewobject | 类 class | 将用户Model转化为可供UI使用的View Object | controller.UserController |
|
| View Object | controller/viewobject | 类 class | 将用户Model转化为可供UI使用的View Object | controller.UserController |
|
||||||
@@ -32,6 +32,8 @@
|
|||||||
| | | | | |
|
| | | | | |
|
||||||
| **其他** | | | | |
|
| **其他** | | | | |
|
||||||
| response | response | | 用于处理HTTP请求返回 | response.CommonReturnType |
|
| response | response | | 用于处理HTTP请求返回 | response.CommonReturnType |
|
||||||
|
| ValidationResult | validator | 类 class | Hibernate Validator 参数验证 | validator\ValidationResult.java |
|
||||||
|
| ValidatorImpl | validator | ValidationResult类实现类 | Hibernate Validator 参数验证 | validator\ValidatorImpl.java |
|
||||||
| | | | | |
|
| | | | | |
|
||||||
| **异常处理** | 用于返回错误信息 | | | |
|
| **异常处理** | 用于返回错误信息 | | | |
|
||||||
| CommonError | error | 接口 interface | | error.CommonError |
|
| CommonError | error | 接口 interface | | error.CommonError |
|
||||||
|
17
pom.xml
17
pom.xml
@@ -65,6 +65,13 @@
|
|||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<version>3.7</version>
|
<version>3.7</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--参数验证-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate</groupId>
|
||||||
|
<artifactId>hibernate-validator</artifactId>
|
||||||
|
<version>5.4.3.Final</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@@ -152,6 +159,16 @@
|
|||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</pluginManagement>
|
</pluginManagement>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>8</source>
|
||||||
|
<target>8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
|
@@ -8,6 +8,8 @@ import com.cxyxiaomo.flashsale.error.BusinessException;
|
|||||||
import com.cxyxiaomo.flashsale.error.EmBusinessError;
|
import com.cxyxiaomo.flashsale.error.EmBusinessError;
|
||||||
import com.cxyxiaomo.flashsale.service.UserService;
|
import com.cxyxiaomo.flashsale.service.UserService;
|
||||||
import com.cxyxiaomo.flashsale.service.model.UserModel;
|
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.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -20,9 +22,13 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserDOMapper userDOMapper;
|
private UserDOMapper userDOMapper;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserPasswordDOMapper userPasswordDOMapper;
|
private UserPasswordDOMapper userPasswordDOMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ValidatorImpl validator;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserModel getUserById(Integer id) {
|
public UserModel getUserById(Integer id) {
|
||||||
// 调用UserDataObjectMapper获取到对应的用户DataObject
|
// 调用UserDataObjectMapper获取到对应的用户DataObject
|
||||||
@@ -60,11 +66,11 @@ public class UserServiceImpl implements UserService {
|
|||||||
if (userModel == null) {
|
if (userModel == null) {
|
||||||
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
|
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
|
||||||
}
|
}
|
||||||
if (StringUtils.isEmpty(userModel.getName())
|
|
||||||
|| userModel.getGender() == null
|
// (优化校验规则)
|
||||||
|| userModel.getAge() == null
|
ValidationResult result = validator.validate(userModel);
|
||||||
|| StringUtils.isEmpty(userModel.getTelephone())) {
|
if (result.isHasErrors()) {
|
||||||
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
|
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, result.getErrMsg());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 实现 model -> dataobject 方法
|
// 实现 model -> dataobject 方法
|
||||||
|
@@ -1,14 +1,33 @@
|
|||||||
package com.cxyxiaomo.flashsale.service.model;
|
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 {
|
public class UserModel {
|
||||||
private Integer id;
|
private Integer id;
|
||||||
|
|
||||||
|
@NotBlank(message = "用户名不能为空")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
@NotNull(message = "姓名不能不填写")
|
||||||
private Byte gender;
|
private Byte gender;
|
||||||
|
|
||||||
|
@NotNull(message = "年龄不能不填写")
|
||||||
|
@Min(value = 0, message = "年龄必须大于0")
|
||||||
|
@Max(value = 150, message = "年龄必须小于150")
|
||||||
private Integer age;
|
private Integer age;
|
||||||
|
|
||||||
|
@NotBlank(message = "手机号不能为空")
|
||||||
private String telephone;
|
private String telephone;
|
||||||
|
|
||||||
private String registerMode;
|
private String registerMode;
|
||||||
|
|
||||||
private String thirdPartyId;
|
private String thirdPartyId;
|
||||||
|
|
||||||
|
@NotBlank(message = "密码不能为空")
|
||||||
private String encryptPassword;
|
private String encryptPassword;
|
||||||
|
|
||||||
public Integer getId() {
|
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