You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.7 KiB
55 lines
1.7 KiB
6 years ago
|
package com.example.frame.utils;
|
||
|
|
||
|
import com.example.frame.base.BaseRequest;
|
||
|
import com.example.frame.base.BaseResponse;
|
||
|
import com.example.frame.base.ErrorType;
|
||
|
import javax.validation.ConstraintViolation;
|
||
|
import javax.validation.Validation;
|
||
|
import javax.validation.Validator;
|
||
|
import javax.validation.ValidatorFactory;
|
||
|
import java.text.ParseException;
|
||
|
import java.text.SimpleDateFormat;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.LinkedList;
|
||
|
import java.util.List;
|
||
|
import java.util.Set;
|
||
|
import java.util.regex.Matcher;
|
||
|
import java.util.regex.Pattern;
|
||
|
|
||
|
/**
|
||
|
* 验证工具类。提供一些通用简单的数据验证功能
|
||
|
*
|
||
|
* @author wangbing
|
||
|
* @version 0.0.1
|
||
|
* @since 2017-01-01
|
||
|
*/
|
||
|
public class ValidationUtil {
|
||
|
|
||
|
private static ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||
|
|
||
|
public static <T extends BaseResponse> T validate(BaseRequest req, T response) {
|
||
|
|
||
|
if (req == null) {
|
||
|
response.addError(ErrorType.EXPECTATION_NULL, "请求对象不能为空");
|
||
|
return response;
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
Validator validator = factory.getValidator();
|
||
|
|
||
|
Set<ConstraintViolation<BaseRequest>> constraintViolations = validator.validate(req);
|
||
|
|
||
|
if (constraintViolations.size() > 0) {
|
||
|
for (ConstraintViolation<BaseRequest> violation : constraintViolations) {
|
||
|
response.addError(ErrorType.INVALID_PARAMETER, violation.getMessage());
|
||
|
}
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
LogUtil.dumpException(e);
|
||
|
response.addError(ErrorType.BUSINESS_ERROR, e.getMessage());
|
||
|
}
|
||
|
return response;
|
||
|
}
|
||
|
}
|