av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

java實現表單必填參數驗證的方法

瀏覽:23日期:2022-08-12 17:43:14
一. 概述

在開發后端接口, 通常都會涉及檢驗參數必填校驗, 一般我們的處理都是很粗暴的寫個if()判斷, 然后拋異常. 本文將介紹通過代理的思想, 用注解優雅的處理非空判斷

二. 實現過程

最終想要的效果->在方法的參數加個注解或者參數的屬性里加個注解, 注解可以自定義報錯信息, 就可以實現自動非空校驗

2.1 編寫注解

@Target({ElementType.FIELD}) //作用的位置@Retention(RetentionPolicy.RUNTIME) //作用域@Documentedpublic @interface NotNull { String value() default '{報錯信息}';}

說明: 該注解用來綁定某個必填屬性

@Target({ElementType.TYPE,ElementType.METHOD}) //作用的位置@Retention(RetentionPolicy.RUNTIME) //作用域@Documentedpublic @interface CheckParam {}

說明: 該注解用來綁定某個類或某個方法,作為校驗代理攔截的標識

2.2 編寫校驗代理AOP

@Aspect@Slf4jpublic class CheckParamAop { @Around('@within(com.midea.cloud.common.annotation.CheckParam) || @annotation(com.midea.cloud.common.annotation.CheckParam)') public Object cacheClear(ProceedingJoinPoint pjp) throws Throwable {try { MethodSignature signature = (MethodSignature) pjp.getSignature(); // 方法參數注解類型 Annotation[][] parameterAnnotations = signature.getMethod().getParameterAnnotations(); // 方法參數的類型 Class<?>[] parameterTypes = signature.getMethod().getParameterTypes(); // 獲取方法參數 Object[] args = pjp.getArgs(); if(!ObjectUtils.isEmpty(args)){// 遍歷參數AtomicInteger index = new AtomicInteger(0);Arrays.stream(args).forEach(o -> { int indexNo = index.getAndAdd(1); /** * 檢查方法參數非空 */ Annotation[] parameterAnnotation = parameterAnnotations[indexNo]; if(!ObjectUtils.isEmpty(parameterAnnotation)){Arrays.stream(parameterAnnotation).forEach(annotation -> { if(annotation instanceof NotNull){NotNull notNull = (NotNull)annotation;// 注解信息String message = notNull.value();// 通過工具類獲取多語言信息String localeMsg = LocaleHandler.getLocaleMsg(message);// 檢查參數非空Optional.ofNullable(o).filter(o1 -> !ObjectUtils.isEmpty(o1)).orElseThrow(()->new BaseException(localeMsg)); }}); } /** * 檢查方法參數屬性非空 */ Class<?> parameterType = parameterTypes[indexNo]; Field[] fields = parameterType.getDeclaredFields(); if(!ObjectUtils.isEmpty(fields)){// 遍歷屬性Arrays.stream(fields).forEach(field -> { NotNull annotation = field.getAnnotation(NotNull.class); if(null != annotation){Object value = null;// 注解信息String message = annotation.value();// 通過工具類獲取多語言信息String localeMsg = LocaleHandler.getLocaleMsg(message);Optional.ofNullable(o).orElseThrow(()->new BaseException(localeMsg));try { field.setAccessible(true); value = field.get(o);} catch (Exception e) { log.error('獲取屬性值報錯'+e.getMessage()); log.error('獲取屬性值報錯'+e);}// value為空時報錯Optional.ofNullable(value).filter(o1 -> !ObjectUtils.isEmpty(o1)).orElseThrow(()->new BaseException(localeMsg)); }}); }}); }} catch (BaseException e) { throw e;} catch (Exception e){ log.error('檢查參數aop報錯:'+e.getMessage()); log.error('檢查參數aop報錯:'+e);}return pjp.proceed(); }}三. 使用示例

public class Test{ @Data class Demo{@NotNull('名字不能為空!')private String name;private String sex;private Integer age; } @CheckParam public void testNoNullCheck1(Demo demo) { } @CheckParam public void testNoNullCheck2(@NotNull('user不能為空') User user) { }}

到此這篇關于java實現表單必填參數驗證的方法的文章就介紹到這了,更多相關java 表單必填參數驗證內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 小草久久久久久久久爱六 | 91精品在线播放 | 国产午夜精品一区二区三区四区 | 青青草在线播放 | 久久久一区二区三区 | 亚洲aⅴ| 久草新在线 | 久久网一区二区 | 国产免费播放视频 | 国产欧美精品一区二区三区 | 久久精品日产第一区二区三区 | 欧美日韩在线一区 | 久久久久久国产 | www久久av| 欧美xxxx网站 | 天天色天天射天天干 | 韩日一区二区三区 | 午夜久久久久 | 亚洲国产欧美一区二区三区久久 | 日韩一级在线 | 99亚洲精品| 中文字幕人成人 | 免费久| 国产成人精品高清久久 | 国产一区精品 | 日韩亚洲视频 | 一级毛片视频 | 国产伦精品一区二区三区照片91 | 精品一区二区三区在线观看国产 | 欧美日韩在线观看视频 | 国产欧美精品一区二区 | 男女羞羞视频在线免费观看 | 91免费在线看 | 中文字幕一区二区三区四区五区 | 免费中文字幕 | 米奇7777狠狠狠狠视频 | 黄色网址在线免费观看 | 精品国产一区二区三区日日嗨 | av在线天天 | 久久久亚洲一区 | 色婷婷一区 |