背景:如果页面表单有很多字段需要提交,因此使用注解校验的方式针对pojo的属性进行校验 因此使用javax标准以及org.hibernate的validator的注解校验
具体的javax的validation的注解以及hibernate的注解都是可以使用的,具体的扩展可以在网上找找,目前在这里就不再进行扩展了。一开始项目使用spring-module 0.9的jar,发现这个鬼东西不支持sprig4,spring4竟然不向下兼容。如果有用这个的可以考虑换成hibernate的注解校验
具体的实现,如下
package validation;
public class BizException extends RuntimeException {
private String errorCode;
private String errorMsg;
public BizException(String errorCode, String errorMsg) {
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public BizException(String message, String errorCode, String errorMsg) {
super(message);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public BizException(String message, Throwable cause, String errorCode, String errorMsg) {
super(message, cause);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public BizException(Throwable cause, String errorCode, String errorMsg) {
super(cause);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public BizException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, String errorCode, String errorMsg) {
super(message, cause, enableSuppression, writableStackTrace);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
}
package validation;
public enum ErrorCodeEnum {
ILLEGAL_PARAM("ILLEGAL_PARAM","服务正忙,请稍后再来");
;
private String errorCode;
private String errorMsg;
ErrorCodeEnum(String errorCode, String errorMsg) {
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public ErrorCodeEnum getErrorCode(String errorCode){
for(ErrorCodeEnum code:values()){
if(code.getErrorCode().equals(errorCode)){
return code;
}
}
return null;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
}
package validation;
import org.springframework.util.CollectionUtils;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Iterator;
import java.util.Set;
public class ValidatorUtil {
private static final Validator validator= Validation.buildDefaultValidatorFactory().getValidator();
public static <T> String validate(T object){
Set<ConstraintViolation<T>> violationSet=validator.validate(object);
if(violationSet!=null&&!CollectionUtils.isEmpty(violationSet)){
Iterator<ConstraintViolation<T>> iterator=violationSet.iterator();
StringBuffer sb=new StringBuffer(64);
while(iterator.hasNext()){
sb.append(iterator.next().getMessage()).append(";");
}
return sb.toString();
}
return null;
}
}
package validation;
public interface ValidatorWrapper {
public void validate(Object object);
}
package validation;
import org.springframework.util.StringUtils;
public class ValidatorWrapperImpl implements ValidatorWrapper {
@Override
public void validate(Object object) {
if(object==null){
throw new RuntimeException();
}
String msg=ValidatorUtil.validate(object);
if(!StringUtils.isEmpty(msg)){
throw new BizException(ErrorCodeEnum.ILLEGAL_PARAM.getErrorCode(),ErrorCodeEnum.ILLEGAL_PARAM.getErrorMsg());
}
}
}
package validation.shopdetail;
import org.hibernate.validator.constraints.NotBlank;
import javax.validation.constraints.NotNull;
public class ShopInfo {
@NotNull(message="shopid不能为空")
private String shopId;
@NotBlank(message="shopName不能为空")
private String shopName;
private String telphone;
public String getShopId() {
return shopId;
}
public void setShopId(String shopId) {
this.shopId = shopId;
}
public String getShopName() {
return shopName;
}
public void setShopName(String shopName) {
this.shopName = shopName;
}
public String getTelphone() {
return telphone;
}
public void setTelphone(String telphone) {
this.telphone = telphone;
}
}
package validation.shopdetail;
import validation.BizException;
import validation.ValidatorWrapper;
import validation.ValidatorWrapperImpl;
public class ShopDeatilTest {
public static void main(String[] args){
ShopInfo shopInfo=new ShopInfo();
shopInfo.setShopId("112");
ValidatorWrapper validatorWrapper=new ValidatorWrapperImpl();
try{
validatorWrapper.validate(shopInfo);
}catch (BizException e){
System.out.println(e.getErrorCode()+"--"+e.getErrorMsg());
}
}
}