Java对象校验框架之Oval

编写登录 注册等功能的时候后台经行校验,可以使用oval框架,简单实用

package com.meadin.main.request;


import com.meadin.module.base.BaseRequest;


import net.sf.oval.constraint.Length;
import net.sf.oval.constraint.NotEmpty;
import net.sf.oval.constraint.NotNull;


public class VisitorRequest extends BaseRequest {
@NotNull
@NotEmpty
private String contacts;
@NotNull
@NotEmpty
@Length(min = 11, max = 11)//电话号码长度的验证
private String phone;
@NotNull
@NotEmpty
private String company;
@NotNull
@NotEmpty
private String duty;
@NotNull
@NotEmpty
private long systemCode;
@NotNull
@NotEmpty

@Length( max = 100)
private String remak;


public String getContacts() {
return contacts;
}


public void setContacts(String contacts) {
this.contacts = contacts == null ? null : contacts.trim();
}


public String getPhone() {
return phone;
}


public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}


public String getCompany() {
return company;
}


public void setCompany(String company) {
this.company = company == null ? null : company.trim();
}


public String getDuty() {
return duty;
}


public void setDuty(String duty) {
this.duty = duty == null ? null : duty.trim();
}


public long getSystemCode() {
return systemCode;
}


public void setSystemCode(long systemCode) {
this.systemCode = systemCode;
}


public String getRemak() {
return remak;
}


public void setRemak(String remak) {
this.remak = remak == null ? null : remak.trim();
}


}

校验的代码



@RequestMapping(value = "/")
@ResponseBody
public Object submit(VisitorRequest visitor, String callback) throws Exception {
// 对传入的对象经行验证
Validator validator = new Validator();
List<ConstraintViolation> list = validator.validate(visitor);

//只要返回集合中有元素就说明有不合法的参数
if (list.size() > 0) {
String result = callback + "(" + JSONObject.toJSON(ApiResult.build(0, "参数错误!!!")).toString() + ");";
return result;
}

}

     欢迎大家关注我的微信公众号 您的关注就是我不懈的动力 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值