my.suveng.util.validate.ValidateUtil Maven / Gradle / Ivy
package my.suveng.util.validate;
import cn.hutool.core.lang.caller.CallerUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import my.suveng.model.common.Message;
import my.suveng.model.common.interfaces.SpringUtil;
import my.suveng.util.log.PlusLogFactoryHutool;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;
/**
* 参数校验
* @author suwenguang
**/
@Component
public class ValidateUtil implements SpringUtil {
@Autowired
Validator validator;
//hutool日志
private static final Log log = LogFactory.setCurrentLogFactory(new PlusLogFactoryHutool()).getLog(CallerUtil.getCaller().getName());
/**
* 参数校验,有返回
* @author suwenguang
*/
Message validate(T object) {
Set> validate = validator.validate(object);
if (validate.size() > 0) {
for (ConstraintViolation e : validate) {
String msg = "参数校验不通过:" + e.getPropertyPath() + e.getMessage();
log.error(msg);
return Message.buildError(msg);
}
}
return Message.buildSuccess(object);
}
}