不在controller中验证,而是在service中验证。

spring boot 默认使用的就是hibernate validator,存在于pom的spring-boot-starter-web中。

1、Validators

import java.util.List;
import java.util.Set; import javax.validation.ConstraintViolation;
import javax.validation.Validator; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
/**
* 通用验证方法
*/
@Service
public class Validators {
@Autowired
private Validator validator; public <T> String valid(T obj) {
return this.valid(obj,null);
}
public <T> String valid(T obj,Class group) {
Set<ConstraintViolation<T>> violations = null;
if (group == null) {
violations = validator.validate(obj);
} else {
violations = validator.validate(obj,group);
}
if (!violations.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (ConstraintViolation<T> constraintViolation : violations) {
sb.append("[").append(constraintViolation.getPropertyPath().toString()).append("=");
sb.append(constraintViolation.getInvalidValue());
sb.append(",");
sb.append(constraintViolation.getMessage());
sb.append("]");
}
return sb.toString();
} else {
return null;
}
}
public <T> String validList(List<T> objList) {
return validList(objList,null);
}
public <T> String validList(List<T> objList,Class group) {
if (CollUtil.isEmpty(objList) || objList.size()<=0) {
return "对象空";
}
StringBuilder sb = new StringBuilder();
String result = "";
for (int i = 0; i < objList.size(); i++) {
if (group == null) {
result = this.valid(objList.get(i));
} else {
result = this.valid(objList.get(i),group);
}
if (!StrUtil.isBlank(result)) {
sb.append(result);
}
}
if (!StrUtil.isBlankIfStr(sb)) {
return sb.toString();
} else {
return null;
}
}
}

2、entity注解

public class PrjDto extends BaseDtoAndApi {
@JSONField(name = "name", ordinal = 11)
@NotBlank(groups = PrjAddInfoReqChecker.class)
private String name; @JSONField(name = "mem_list", ordinal = 24)
@NotEmpty
List<PrjMemberDto> memList;
}

3、PrjAddInfoReqChecker

public interface PrjAddInfoReqChecker {
}

4、service

    @Autowired
private Validators validators; @Transactional(readOnly=false)
public ReMsg addPrj(PrjAddInfoReq prjAddInfoReq) {
log.info("接收:{}",JSONObject.toJSONString(prjAddInfoReq,true));
ReMsg reMsg = null;
Integer rs = null; String validResult = validators.valid(prjAddInfoReq,PrjAddInfoReqChecker.class);
if (!StrUtil.isBlankIfStr(validResult)) {
reMsg = new ReMsg("999",validResult);
return reMsg;
}
//验证集合中的对象
String validListResult1 = validators.validList(prjAddInfoReq.getMemList(),PrjAddInfoReqChecker.class);
if (!StrUtil.isBlankIfStr(validListResult1)) {
reMsg = new ReMsg("999",validResult);
return reMsg;
}
......
}

最新文章

  1. python将图片转换为Framebuffer裸数据格式(终端显示图片)
  2. Android first---放置在外存中的文件读取
  3. switch语句和for循环
  4. 百度web前端面试2015.10.18
  5. [Redux] Fetching Data on Route Change
  6. hdoj(3790) 最短路径
  7. Java多线程——线程池
  8. 环形进度条带数字显示(canvas)
  9. SQL Server 查看表定义的 2 种方法
  10. java之观察者模式
  11. Spring Security(10)——退出登录logout
  12. Python标准库之ConfigParser模块
  13. Linux基础学习(11)--Shell编程
  14. CentOS 与 Ubuntu 使用命令搭建 LAMP 环境
  15. The Little Prince-11/26
  16. mysql unix domain socket and network socket, ssh key
  17. python基础之02列表/元组/字典/set集合
  18. WhyEngine游戏引擎作品合集
  19. 在Centos7下安装与部署.net core
  20. 接口自动化&#183;分享&#183;第二篇&#183;你必须了解的HttpRequest和HttpResponse

热门文章

  1. &lt;C++&gt;友元与虚函数的组合
  2. 集训Day4
  3. bootstrap框架日期时间之年月选择及汉化
  4. zero to one (3)
  5. javacpp-FFmpeg系列补充:FFmpeg拉流截图实现在线演示demo(视频截图并返回base64图像,支持jpg/png/gif/bmp等多种格式)
  6. css3渐变gradient
  7. 设置一个.exe文件开机启动
  8. [codeforces161D]Distance in Tree(点分治/树形dp)
  9. oracle 查询及删除重复记录的SQL语句
  10. Biopython常用功能模块