最近公司重構項目,重構為最熱的微服務框架 spring boot, 重構的時候遇到幾個可以統一處理的問題,也是項目中經常遇到,列如:統一校驗參數,統一捕獲異常。。。
僅憑代碼 去控制參數的校驗,有時候是冗余的,但通過框架支持的 去控制參數的校驗,是對于開發者很友好,先看下面的例子
1
2
3
4
|
@notempty (message= "手機號不能為空" ) @size (min= 11 ,max= 11 ,message= "手機號碼長度不正確" ) @pattern (regexp=stringutils.regexp_mobile,message= "手機號格式不正確" ) private string mobile; |
這是spring boot支持的 校驗注解,然后我們在 contoller層 加上@valid 注解 就可以達到校驗的目的。這是一種框架自帶的
本章 就展示一種 自定義的 aop 校驗,首先 寫一個注解,注解里面可以寫上 我們需要校驗的規則, 比如長度,正則。。。
1
2
3
4
5
6
7
8
9
10
11
12
|
@documented @target ({elementtype.field,elementtype.method}) @retention (retentionpolicy.runtime) public @interface validateparam { int min() default 0 ; int max() default integer.max_value; string message() default "params is not null" ; string regexp(); class <?>[] groups() default { }; class <? extends payload>[] payload() default { }; boolean isnotnull() default true ; } |
然后定義一個aop類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
package com.onecard.primecard.common.aop; import java.lang.reflect.field; import java.lang.reflect.method; import java.lang.reflect.parameterizedtype; import java.util.arraylist; import java.util.arrays; import java.util.regex.pattern; import org.aspectj.lang.joinpoint; import org.aspectj.lang.proceedingjoinpoint; import org.aspectj.lang.annotation.around; import org.aspectj.lang.annotation.aspect; import org.aspectj.lang.annotation.before; import org.aspectj.lang.annotation.pointcut; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import org.springframework.stereotype.component; import com.jfcf.core.dto.resultdata; import com.onecard.core.support.util.stringutils; import com.onecard.primecard.common.annotation.validateparam; import com.onecard.primecard.common.utils.resultdatautil; /** * 全局 切面類(校驗參數) * * @author administrator * */ @aspect @component public class gobalhandleraspect { private static logger logger = loggerfactory.getlogger(gobalhandleraspect. class ); @pointcut ( "execution(* 包名.controller..*.*(..)) && execution(* 包名.controller..*.*(..))" ) public void checkaspect(){}; @before ( "checkaspect()" ) public void befor(joinpoint joinpoint) throws exception{ //前置統一輸出參數 object[] args = joinpoint.getargs(); if (args != null && args.length> 0 ){ object obj = args[ 0 ]; parameterizedtype pt = (parameterizedtype)obj.getclass().getgenericsuperclass(); class <?> classzz = ( class <?>) pt.getactualtypearguments()[ 0 ]; logger.info( "【小x卡】-【請求實體入參】:" +classzz.newinstance().tostring()); } } @around ( "checkaspect()" ) public object around(proceedingjoinpoint joinpoint) throws throwable{ //校驗參數 object[] args = joinpoint.getargs(); object obj = null ; if (args != null && args.length > 0 ){ obj = args[ 0 ]; class classzz = obj.getclass(); //沒有順序和秩序的數組 field[] fieldarray = classzz.getdeclaredfields(); arraylist<field> fieldlist = new arraylist<field>(arrays.aslist(fieldarray)); string res = checkparam(fieldlist,obj); if (stringutils.isnotnull(res)){ return resultdatautil.result(resultdata.status_param_error, res); } } return joinpoint.proceed(); } private string checkparam(arraylist<field> fieldlist, object obj) throws exception { for (field field : fieldlist){ validateparam validateparam = field.getannotation(validateparam. class ); logger.info( "【小x卡】獲取注解值:" +validateparam.isnotnull()+ "min=" +validateparam.min()+ "max=" +validateparam.max()); method method = obj.getclass().getmethod( "get" +getmethodname(field.getname())); logger.info( "【小x卡】入參實體方法名稱:" +method.getname()); if (method != null ){ object val = method.invoke(obj); logger.info( "【小x卡】回調方法:" +val); if (validateparam != null && validateparam.isnotnull() == true ){ if ( null == val || "" .equals(val) ){ return field.getname()+ "必填參數為空" ; } } if (validateparam.min()== 11 && validateparam.max() == 11 ){ if (val.tostring().length() != 11 ){ return field.getname()+ "請輸入參數正確的長度" ; } } if (validateparam.regexp().equals(stringutils.regexp_mobile)){ if (!pattern.matches(stringutils.regexp_mobile, val.tostring())){ return field.getname()+ "參數格式錯誤" ; } } } } return null ; } /** * 方法首字母大寫 * @param fieldname * @return */ private string getmethodname(string fieldname) { stringbuffer buffer = new stringbuffer(); string firstletter = fieldname.substring( 0 , 1 ).touppercase(); return buffer.append(firstletter).append(fieldname.substring( 1 , fieldname.length())).tostring(); } } |
定義一個切點 @pointcut, 用execution 表達式,去獲取要校驗的 某個類 和某個方法, 也就是連接點,然后 用定義一個通知,上面代碼中有2個通知,一個前置通知@before,一個環繞通知@around,我們使用功能最強大的環繞通知。
通過上面的代碼可以看出 首先獲取參數,然后通過反射機制 獲取 入參對象中的全部字段, 再去獲取 我們在字段中加 我們自定義注解的字段,通過反射方法的回調,獲取字段值,對值做判斷, 返回校驗結果。
總結
以上所述是小編給大家介紹的spring boot+自定義 aop 實現全局校驗的實例代碼,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
原文鏈接:https://www.cnblogs.com/dream-sun/p/10677350.html