SpringBoot_请求参数校验_validator

依赖pom.xml

配置文件bootstrap.yml

配置源码

对象校验

/**
 * 更新数据 Group
 * 
 * 
 * @author vander
 * @date 2018年11月29日
 */
public interface UpdateGroup {
}
/**
 * 新增数据 Group
 * 
 * 
 * @author vander
 * @date 2018年11月29日
 */
public interface AddGroup {
}


//实体类上添加注解
//@NotBlank(message="邮箱不能为空", groups = {AddGroup.class, UpdateGroup.class})
//@Email(message="邮箱格式不正确", groups = {AddGroup.class, UpdateGroup.class})

//业务代码校验请求参数如下:
///**
// * 根据id编辑页面数据
// * @param id
// * @param cmsPage
// * @return
// */
//@Override
//@PutMapping("/edit/{id}")
//public RestResult edit(@PathVariable("id") String id, @RequestBody CmsPage cmsPage) {
//	Assert.isNotBlank(id);
//	ValidatorUtils.validateEntity(cmsPage);//校验请求数据
//	long result = cmsPageService.edit(id,cmsPage);
//	return ok(result);
//}
/**
 * hibernate-validator校验工具类
 * 
 * 
 * @author vander
 * @date 2018年11月28日
 */
public class ValidatorUtils {
	
    private static Validator validator;

    static {
        validator = Validation.buildDefaultValidatorFactory().getValidator();
    }

    /**
     * 校验对象
     * @param object        待校验对象
     * @param groups        待校验的组
     * @throws BException  校验不通过,则报BException异常
     */
    public static void validateEntity(Object object, Class<?>... groups){
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
        if (!constraintViolations.isEmpty()) {
            StringBuilder msg = new StringBuilder();
            for(ConstraintViolation<Object> constraint:  constraintViolations){
                msg.append(constraint.getMessage()).append("<br>");
            }
            throw new BException(msg.toString(),400);
        }
    }
}

参数校验

/**
 * 数据校验
 * 
 * 
 * @author vander
 * @date 2018年11月29日
 */
public abstract class Assert {

	public static void isNotBlankSys(String str, String message) {
		if (StringUtils.isBlank(str)) {
			throw new BException(message, 500);
		}
	}

	public static void isNotBlank(String str, String message) {
		if (StringUtils.isBlank(str)) {
			throw new BException(message, 400);
		}
	}

	public static void isNotBlank(String str) {
		if (StringUtils.isBlank(str)) {
			throw new BException(Code.PARAM_ERROR);
		}
	}

	public static void isNotNull(Object object, String message) {
		if (object == null) {
			throw new BException(message, 400);
		}
	}
	
	public static void isNotEmpty(@SuppressWarnings("rawtypes") Collection collection) {
		if(collection==null || collection.size()==0) {
			throw new BException(Code.PARAM_ERROR);
		}
	}
	
	public static void isNotEmpty(@SuppressWarnings("rawtypes")Collection collection, String message) {
		if(collection==null || collection.size()==0) {
			throw new BException(message, 400);
		}
	}
	
	public static void isNotEmpty(@SuppressWarnings("rawtypes") Map map) {
		if(map==null || map.size()==0) {
			throw new BException(Code.PARAM_ERROR);
		}
	}
	
	public static void isNotEmpty(@SuppressWarnings("rawtypes")Map map, String message) {
		if(map==null || map.size()==0) {
			throw new BException(message, 400);
		}
	}

	public static void isNotNull(Object object) {
		if (object == null) {
			throw new BException(Code.PARAM_ERROR);
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_15764943/article/details/87804074