转载,介绍的简单直观
http://www.cnblogs.com/huihui0630/archive/2012/09/07/2675575.html
只要有接口,就会有参数的校验,目前开源的校验框架已经非常多了,不过不得不提一下Oval。
OVal 是一个可扩展的Java对象数据验证框架,验证的规则可以通过配置文件、Annotation、POJOs 进行设定。可以使用纯 Java 语言、JavaScript 、Groovy 、BeanShell 等进行规则的编写。
使用起来也非常简单
public class OvalTest { @Min(20) private int age; @Length(min = 6, max = 10) private String name; public static void main(String[] args) { OvalTest ovalTest = new OvalTest(); ovalTest.age = 10; ovalTest.name = "kolor"; Validator validator = new Validator(); List<ConstraintViolation> ret = validator.validate(ovalTest); System.out.println(ret); } }
校验自然是不过的了,输出如下:
[net.sf.oval.ConstraintViolation: OvalTest.age cannot be smaller than 20.0, net.sf.oval.ConstraintViolation: OvalTest.name is not between 6 and 10 characters long]
maven依赖:
<dependency> <groupId>net.sf.oval</groupId> <artifactId>oval</artifactId> <version>1.81</version> </dependency>
Oval官方地址: http://oval.sourceforge.net/
=============================
Qbao enterprise demo
package com.enterprise.domain; import net.sf.oval.constraint.MaxLength; import net.sf.oval.constraint.MinLength; import net.sf.oval.constraint.NotEmpty; import net.sf.oval.constraint.NotNull; import java.util.Date; public class MerchantShopInfo { public MerchantShopInfo() { } public MerchantShopInfo(String shopName, long merchantId, String shopContactUser, String contactPhone) { this.shopName = shopName; this.merchantId = merchantId; this.shopContactUser = shopContactUser; this.contactPhone = contactPhone; } public MerchantShopInfo(long id, long merchantId, String shopName, String shopContactUser, String contactPhone) { this.id = id; this.merchantId = merchantId; this.shopName = shopName; this.shopContactUser = shopContactUser; this.contactPhone = contactPhone; } public MerchantShopInfo(String logoPath,long merchantId,long id) { this.logoPath = logoPath; this.merchantId = merchantId; this.id = id; } private long id; @NotNull(message = "商家ID不能为空") @NotEmpty(message = "商家ID不能为空") private long merchantId;//商家Id @NotNull(message = "店铺名称不能为空") @NotEmpty(message = "店铺名称不能为空") @MaxLength(value = 50,message = "店铺名称最多50个字") private String shopName;//商家名称 private String logoPath;//店铺logo private String provinceCityArea; private String street;//店铺地址 private String shopMainBusiness;//店铺主营业务 @NotNull(message = "店铺联系人不能为空") @NotEmpty(message = "店铺联系人不能为空") @MaxLength(value = 50,message = "店铺联系人最多10个字") private String shopContactUser;//店铺联系人 @NotNull(message = "联系人手机不能为空") @NotEmpty(message = "联系人手机不能为空") @MaxLength(value = 11,message = "联系人手机最多11个字") private String contactPhone;//联系人手机 private String contactEmail;//联系人邮箱 @NotNull(message = "公众号简介不能为空") @NotEmpty(message = "公众号简介不能为空") @MaxLength(value = 120,message = "公众号简介最多120个字") @MinLength(value = 4,message = "公众号简介最少4个字") private String shopDesc;//公众号简介/店铺简介 private String contactFax;//联系人传真 private String netShopRegistCode;//网店工商营业执照注册号 private String netRegistImgPath;//网店营业执照 private String netBusinessScope;//网店营业执照经营范围 private Date netPeriodValidityStart;// 网店营业执照有效期 private Date netPeriodValidityEnd;//网店营业执照有效期 private String netTaxImgPath;//网店税务登记证 private Double longitude; private Double latitude; private int merchantType;//1:个人商家,2:企业商家 private String cover;//封面 private String appCover; private Long userId; private String createTime; public long getId() { return id; } public void setId(long id) { this.id = id; } public long getMerchantId() { return merchantId; } public void setMerchantId(long merchantId) { this.merchantId = merchantId; } public String getShopName() { return shopName; } public void setShopName(String shopName) { this.shopName = shopName; } public String getLogoPath() { return logoPath; } public void setLogoPath(String logoPath) { this.logoPath = logoPath; } public String getShopMainBusiness() { return shopMainBusiness; } public void setShopMainBusiness(String shopMainBusiness) { this.shopMainBusiness = shopMainBusiness; } public String getShopContactUser() { return shopContactUser; } public void setShopContactUser(String shopContactUser) { this.shopContactUser = shopContactUser; } public String getContactPhone() { return contactPhone; } public void setContactPhone(String contactPhone) { this.contactPhone = contactPhone; } public String getContactFax() { return contactFax; } public void setContactFax(String contactFax) { this.contactFax = contactFax; } public String getContactEmail() { return contactEmail; } public void setContactEmail(String contactEmail) { this.contactEmail = contactEmail; } public String getShopDesc() { return shopDesc; } public void setShopDesc(String shopDesc) { this.shopDesc = shopDesc; } public String getNetShopRegistCode() { return netShopRegistCode; } public void setNetShopRegistCode(String netShopRegistCode) { this.netShopRegistCode = netShopRegistCode; } public String getNetRegistImgPath() { return netRegistImgPath; } public void setNetRegistImgPath(String netRegistImgPath) { this.netRegistImgPath = netRegistImgPath; } public String getNetBusinessScope() { return netBusinessScope; } public void setNetBusinessScope(String netBusinessScope) { this.netBusinessScope = netBusinessScope; } public Date getNetPeriodValidityStart() { return netPeriodValidityStart; } public void setNetPeriodValidityStart(Date netPeriodValidityStart) { this.netPeriodValidityStart = netPeriodValidityStart; } public Date getNetPeriodValidityEnd() { return netPeriodValidityEnd; } public void setNetPeriodValidityEnd(Date netPeriodValidityEnd) { this.netPeriodValidityEnd = netPeriodValidityEnd; } public String getNetTaxImgPath() { return netTaxImgPath; } public void setNetTaxImgPath(String netTaxImgPath) { this.netTaxImgPath = netTaxImgPath; } public String getCover() { return cover; } public void setCover(String cover) { this.cover = cover; } public String getProvinceCityArea() { return provinceCityArea; } public void setProvinceCityArea(String provinceCityArea) { this.provinceCityArea = provinceCityArea; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public Double getLongitude() { return longitude; } public void setLongitude(Double longitude) { this.longitude = longitude; } public Double getLatitude() { return latitude; } public void setLatitude(Double latitude) { this.latitude = latitude; } public String getShopNameCut() { if (shopName != null && shopName.length() > 15) { shopName = shopName.substring(0, 14) + "..."; } return shopName; } public String getCreateTime() { return createTime; } public void setCreateTime(String createTime) { this.createTime = createTime; } public String getAppCover() { return appCover; } public void setAppCover(String appCover) { this.appCover = appCover; } public int getMerchantType() { return merchantType; } public void setMerchantType(int merchantType) { this.merchantType = merchantType; } public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } }
@Override public void addMerchanCompanyShop(MerchantShopInfo shopInfo) throws DataValidateException { List<MerchantShopInfo> shops = merchantCompanyShopInfoRepository.getMerchantShopInfo(shopInfo.getMerchantId()); if (shops != null && shops.size() > 0) { throw new RuntimeException("目前每个厂家只能有一家店铺!"); } Validator validator = new Validator(); List<ConstraintViolation> errorMsg = validator.validate(shopInfo); if (errorMsg.isEmpty()) { try { merchantCompanyShopInfoRepository.addMerchanCompanyShop(shopInfo); } catch (DuplicateKeyException e) { throw new DataValidateException("店铺名称已经存在"); } catch (Exception e) { log.error("添加(merchantId=" + shopInfo.getMerchantId() + ")店铺信息出错", e); throw new DataValidateException("添加店铺信息出错"); } } else { throw new DataValidateException(errorMsg.get(0).getMessage()); } }