java實(shí)現(xiàn)表單必填參數(shù)驗(yàn)證的方法
一. 概述
在開發(fā)后端接口, 通常都會(huì)涉及檢驗(yàn)參數(shù)必填校驗(yàn), 一般我們的處理都是很粗暴的寫個(gè)if()判斷, 然后拋異常. 本文將介紹通過代理的思想, 用注解優(yōu)雅的處理非空判斷
二. 實(shí)現(xiàn)過程
最終想要的效果->在方法的參數(shù)加個(gè)注解或者參數(shù)的屬性里加個(gè)注解, 注解可以自定義報(bào)錯(cuò)信息, 就可以實(shí)現(xiàn)自動(dòng)非空校驗(yàn)
2.1 編寫注解
@Target({ElementType.FIELD}) //作用的位置 @Retention(RetentionPolicy.RUNTIME) //作用域 @Documented public @interface NotNull { String value() default "{報(bào)錯(cuò)信息}"; }
說明: 該注解用來綁定某個(gè)必填屬性
@Target({ElementType.TYPE,ElementType.METHOD}) //作用的位置 @Retention(RetentionPolicy.RUNTIME) //作用域 @Documented public @interface CheckParam { }
說明: 該注解用來綁定某個(gè)類或某個(gè)方法,作為校驗(yàn)代理攔截的標(biāo)識(shí)
2.2 編寫校驗(yàn)代理AOP
@Aspect @Slf4j public 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(); // 方法參數(shù)注解類型 Annotation[][] parameterAnnotations = signature.getMethod().getParameterAnnotations(); // 方法參數(shù)的類型 Class<?>[] parameterTypes = signature.getMethod().getParameterTypes(); // 獲取方法參數(shù) Object[] args = pjp.getArgs(); if(!ObjectUtils.isEmpty(args)){ // 遍歷參數(shù) AtomicInteger index = new AtomicInteger(0); Arrays.stream(args).forEach(o -> { int indexNo = index.getAndAdd(1); /** * 檢查方法參數(shù)非空 */ 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); // 檢查參數(shù)非空 Optional.ofNullable(o). filter(o1 -> !ObjectUtils.isEmpty(o1)). orElseThrow(()->new BaseException(localeMsg)); } }); } /** * 檢查方法參數(shù)屬性非空 */ 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("獲取屬性值報(bào)錯(cuò)"+e.getMessage()); log.error("獲取屬性值報(bào)錯(cuò)"+e); } // value為空時(shí)報(bào)錯(cuò) Optional.ofNullable(value). filter(o1 -> !ObjectUtils.isEmpty(o1)). orElseThrow(()->new BaseException(localeMsg)); } }); } }); } } catch (BaseException e) { throw e; } catch (Exception e){ log.error("檢查參數(shù)aop報(bào)錯(cuò):"+e.getMessage()); log.error("檢查參數(shù)aop報(bào)錯(cuò):"+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) { } }
到此這篇關(guān)于java實(shí)現(xiàn)表單必填參數(shù)驗(yàn)證的方法的文章就介紹到這了,更多相關(guān)java 表單必填參數(shù)驗(yàn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 實(shí)現(xiàn)將一個(gè)string保存到txt文檔中
今天小編就為大家分享一篇java 實(shí)現(xiàn)將一個(gè)string保存到txt文檔中的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07Java notify和notifyAll的區(qū)別和相同
本文主要介紹Java notify和notifyAll的知識(shí),這里整理詳細(xì)的資料來說明notify 和NotifAll的區(qū)別,有需要的小伙伴可以參考下2016-09-09Java如何優(yōu)雅地避免空指針異常(NullPointerException)
這篇文章主要給大家介紹了關(guān)于Java如何優(yōu)雅地避免空指針異常(NullPointerException)的相關(guān)資料,空指針異常(NullPointerException)是一種常見的運(yùn)行時(shí)異常,它在Java編程中經(jīng)常出現(xiàn),需要的朋友可以參考下2024-03-03簡(jiǎn)單談?wù)刯ava中匿名內(nèi)部類構(gòu)造函數(shù)
這篇文章主要簡(jiǎn)單給我們介紹了java中匿名內(nèi)部類構(gòu)造函數(shù),并附上了簡(jiǎn)單的示例,有需要的小伙伴可以參考下。2015-11-11Java工廠模式用法之如何動(dòng)態(tài)選擇對(duì)象詳解
工廠設(shè)計(jì)模式可能是最常用的設(shè)計(jì)模式之一,我想大家在自己的項(xiàng)目中都用到過。本文不僅僅是關(guān)于工廠模式的基本知識(shí),更是討論如何在運(yùn)行時(shí)動(dòng)態(tài)選擇不同的方法進(jìn)行執(zhí)行,你們可以看看是不是和你們項(xiàng)目中用的一樣2023-03-03java 中使用匿名類直接new接口詳解及實(shí)例代碼
這篇文章主要介紹了java 中使用匿名類直接new接口詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03