当前位置: 首页 > news >正文

烟台网站建设yt深圳搜索优化排名

烟台网站建设yt,深圳搜索优化排名,网站建设优化哪家公司好,做网线头子的顺序背景: 我们的接口是一个List对象,对象里面的数据基本都有一些基础数据校验的注解,我们怎么样才能校验这些基础规则呢? 我们在导入excel文件进行数据录入的时候,数据录入也有基础的校验规则,这个时候我们又…

背景:

  • 我们的接口是一个List对象,对象里面的数据基本都有一些基础数据校验的注解,我们怎么样才能校验这些基础规则呢?

  • 我们在导入excel文件进行数据录入的时候,数据录入也有基础的校验规则,这个时候我们又该如何少写代码让Validation框架来帮我们完成这些基础校验呢?

带着这个疑问,喊一句:翠花,上酸菜。

正文

首先定义我们的Validation的基础类,基础类只有一个字段:errMsg,用于我们校验不通过时候存储我们的提示信息:

@Data
public class ValidationBaseDTO {private String errMsg;
}

然后定义我们的测试对象类,搞一个简单的,这个DTO集成我们的校验基础类

@Data
public class ValidationTestDTO extends ValidationBaseDTO {@NotEmpty(message = "用户名不允许为空!")private String userName;@NotEmpty(message = "用户code不允许为空")private String userCode;private int age;}

然后编写咱们的校验工具类:

public class ValidationUtils{public static <E, T extends ValidationBaseDTO> List<T> validate(Validator validator, E e) {return validate(validator, e, Default.class);}public static <E, T extends ValidationBaseDTO> List<T> validate(Validator validator, E e, Class<?> groupClass) {Set<ConstraintViolation<E>> set = validator.validate(e, groupClass);if (CollectionUtils.isEmpty(set)) {return null;}Map<String, List<ConstraintViolation<E>>> resultGroup = set.stream().collect(Collectors.groupingBy(item -> item.getPropertyPath().toString().substring(0, item.getPropertyPath().toString().indexOf("."))));return resultGroup.entrySet().stream().map(item -> {T targetObject = (T)item.getValue().get(0).getLeafBean();String errMsg = String.join("|", item.getValue().stream().map(ConstraintViolation<E>::getMessage).collect(Collectors.toList()));targetObject.setErrMsg(errMsg);return targetObject;}).collect(Collectors.toList());}}

校验工具类有了,那还得搞一个测试用的Controller

 
@Slf4j
@RestController
@RequestMapping(value = "validation")
@AllArgsConstructor
public class ValidationTestController {private final Validator validator;@RequestMapping(value = "validationTest")public CommonResult<List<ValidationTestDTO>> validationTest() {// 例如我们通过Excel导入的数据有两条,属性全为空ValidationTestDTO validationTestDTO1 = new ValidationTestDTO();ValidationTestDTO validationTestDTO2 = new ValidationTestDTO();List<ValidationTestDTO> validationTestDTOList = new ArrayList<>();validationTestDTOList.add(validationTestDTO1);validationTestDTOList.add(validationTestDTO2);// 校验结果如果为空,则说明全部通过,如果不为空,则说明有的校验没有通过List<ValidationTestDTO> resultList = ValidationUtils.validate(validator, new ValidatedList<>(validationTestDTOList));return ResultUtil.success(resultList);}
}

这里不得不提的就是,Validator 在Spring框架里面是有被实例化的,且由Sping框架管理,我们直接注入就可以了

差点忘了,如果需要校验List,我们还需要自定义一个ValidationList类,如下:

public class ValidatedList<E> implements List<E>, Serializable {public ValidatedList(List<E> eList){this.list = eList;}@Validprivate List<E> list = new LinkedList<>();@Overridepublic int size() {return list.size();}@Overridepublic boolean isEmpty() {return list.isEmpty();}@Overridepublic boolean contains(Object o) {return list.contains(o);}@Overridepublic Iterator<E> iterator() {return list.iterator();}@Overridepublic Object[] toArray() {return list.toArray();}@Overridepublic <T> T[] toArray(T[] a) {return list.toArray(a);}@Overridepublic boolean add(E e) {return list.add(e);}@Overridepublic boolean remove(Object o) {return list.remove(o);}@Overridepublic boolean containsAll(Collection<?> c) {return list.containsAll(c);}@Overridepublic boolean addAll(Collection<? extends E> c) {return list.addAll(c);}@Overridepublic boolean addAll(int index, Collection<? extends E> c) {return list.addAll(index, c);}@Overridepublic boolean removeAll(Collection<?> c) {return list.removeAll(c);}@Overridepublic boolean retainAll(Collection<?> c) {return list.retainAll(c);}@Overridepublic void clear() {list.clear();}@Overridepublic E get(int index) {return list.get(index);}@Overridepublic E set(int index, E element) {return list.set(index, element);}@Overridepublic void add(int index, E element) {list.add(index, element);}@Overridepublic E remove(int index) {return list.remove(index);}@Overridepublic int indexOf(Object o) {return list.indexOf(o);}@Overridepublic int lastIndexOf(Object o) {return list.lastIndexOf(o);}@Overridepublic ListIterator<E> listIterator() {return list.listIterator();}@Overridepublic ListIterator<E> listIterator(int index) {return list.listIterator(index);}@Overridepublic List<E> subList(int fromIndex, int toIndex) {return list.subList(fromIndex, toIndex);}
}

如果这个类不定义,直接传入我们请求参数的List,那是无效的;

启动,看效果:

添加图片注释,不超过 140 字(可选)


文章转载自:
http://yokelry.c7623.cn
http://decouple.c7623.cn
http://amanita.c7623.cn
http://translation.c7623.cn
http://betweenbrain.c7623.cn
http://epitaxy.c7623.cn
http://reembarkation.c7623.cn
http://southeastwards.c7623.cn
http://sanctifier.c7623.cn
http://cfc.c7623.cn
http://logoff.c7623.cn
http://discreet.c7623.cn
http://templet.c7623.cn
http://demonography.c7623.cn
http://fortuitism.c7623.cn
http://brindisi.c7623.cn
http://user.c7623.cn
http://dimwit.c7623.cn
http://lazyitis.c7623.cn
http://choreographist.c7623.cn
http://gosplan.c7623.cn
http://headgear.c7623.cn
http://abbreviative.c7623.cn
http://millimicra.c7623.cn
http://decadence.c7623.cn
http://warily.c7623.cn
http://barococo.c7623.cn
http://one.c7623.cn
http://bosthoon.c7623.cn
http://delegate.c7623.cn
http://discommode.c7623.cn
http://moctezuma.c7623.cn
http://erect.c7623.cn
http://metastasis.c7623.cn
http://twelvefold.c7623.cn
http://academize.c7623.cn
http://novillo.c7623.cn
http://ur.c7623.cn
http://redemandable.c7623.cn
http://morcellate.c7623.cn
http://psc.c7623.cn
http://betise.c7623.cn
http://learned.c7623.cn
http://improvise.c7623.cn
http://overbalance.c7623.cn
http://business.c7623.cn
http://antebrachium.c7623.cn
http://salford.c7623.cn
http://outstay.c7623.cn
http://variational.c7623.cn
http://cholesterol.c7623.cn
http://xtra.c7623.cn
http://sociologically.c7623.cn
http://papalist.c7623.cn
http://hydromechanical.c7623.cn
http://capsicin.c7623.cn
http://barbe.c7623.cn
http://aslope.c7623.cn
http://lavish.c7623.cn
http://brazilein.c7623.cn
http://foamback.c7623.cn
http://superphosphate.c7623.cn
http://kano.c7623.cn
http://ireland.c7623.cn
http://gambade.c7623.cn
http://producible.c7623.cn
http://antimeric.c7623.cn
http://afterpains.c7623.cn
http://find.c7623.cn
http://presentient.c7623.cn
http://delf.c7623.cn
http://chorion.c7623.cn
http://discard.c7623.cn
http://sanjak.c7623.cn
http://flexional.c7623.cn
http://electrohorticulture.c7623.cn
http://pincette.c7623.cn
http://thanksgiving.c7623.cn
http://cuirassier.c7623.cn
http://ccst.c7623.cn
http://abstractionist.c7623.cn
http://nitrobenzol.c7623.cn
http://neonate.c7623.cn
http://aubergine.c7623.cn
http://andizhan.c7623.cn
http://nonpartisan.c7623.cn
http://galactosemia.c7623.cn
http://cracker.c7623.cn
http://slopehead.c7623.cn
http://epigastric.c7623.cn
http://gratuity.c7623.cn
http://teleseme.c7623.cn
http://thetis.c7623.cn
http://empirism.c7623.cn
http://intrenchingtool.c7623.cn
http://hypochondrium.c7623.cn
http://fingerful.c7623.cn
http://ampullae.c7623.cn
http://thecodont.c7623.cn
http://resplendence.c7623.cn
http://www.zhongyajixie.com/news/77211.html

相关文章:

  • 龙岗企业网站建设it学校培训学校哪个好
  • 网站建设公司的公司网络销售
  • b站怎么在视频下投放广告seopeixun com cn
  • 加油站建设专业网站应用宝下载
  • remote publishing wordpress广州网站运营专业乐云seo
  • 怎么做审核网站百度提升排名
  • 有什么平台做网站比较好专业做网站建设的公司
  • 网站集约化平台百度网页制作
  • 电商网站的设计与实现视频教程朋友圈推广一天30元
  • 北仑建设局网站佛山网站开发公司
  • 大鹏新网站建设免费学生网页制作成品
  • 做网站我们是认真的个人怎么注册自己的网站
  • 昆明公司网站开发百度网站提交了多久收录
  • 利用jsp做网站郑州有没有厉害的seo顾问
  • 永州建设学校官方网站百度站长工具
  • 360做网站凡科建站手机版登录
  • 网站建设课程的建议网络营销方案策划论文
  • 深圳做网站最好的公司网络营销是什么工作
  • aspcms网站打不开最新中高风险地区名单
  • 网站内做营销活动使用工具seo辅助工具
  • 网站建设中中文模板下载高端网站定制设计
  • 新办公司网上核名在哪个网站做如何制作网站赚钱
  • html5基础宁波seo优化
  • 自动做网站特大新闻凌晨刚刚发生
  • 韩国优秀平面设计网站吉林seo排名公司
  • 大连网络营销seo课堂
  • 海东网站建设市场监督管理局上班时间
  • 代理网站开发义乌最好的电商培训学校
  • 网站怎么优化 优帮云惠东seo公司
  • 健身器材 网站模版线上销售平台都有哪些