引言
使用springmvc作為controller層進行web開發時,經常會需要對controller中的方法進行參數檢查。本來springmvc自帶@valid和@validated兩個注解可用來檢查參數,但只能檢查參數是bean的情況,對于參數是string或者long類型的就不適用了,而且有時候這兩個注解又突然失效了(沒有仔細去調查過原因),對此,可以利用spring的aop和自定義注解,自己寫一個參數校驗的功能。
代碼示例
注意:本節代碼只是一個演示,給出一個可行的思路,并非完整的解決方案。
本項目是一個簡單web項目,使用到了:spring、springmvc、maven、jdk1.8
項目結構:
自定義注解:
validparam.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.lzumetal.ssm.paramcheck.annotation; import java.lang.annotation.*; /** * 標注在參數bean上,表示需要對該參數校驗 */ @target ({elementtype.parameter}) @retention (retentionpolicy.runtime) @documented public @interface validparam { } |
notnull.java:
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.lzumetal.ssm.paramcheck.annotation; import java.lang.annotation.*; @target ({elementtype.field, elementtype.parameter}) @retention (retentionpolicy.runtime) @documented public @interface notnull { string msg() default "字段不能為空" ; } |
notempty.java:
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.lzumetal.ssm.paramcheck.annotation; import java.lang.annotation.*; @target ({elementtype.field, elementtype.parameter}) @retention (retentionpolicy.runtime) @documented public @interface notempty { string msg() default "字段不能為空" ; } |
切面類
paramcheckaspect.java:
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
|
package com.lzumetal.ssm.paramcheck.aspect; import com.lzumetal.ssm.paramcheck.annotation.notempty; import com.lzumetal.ssm.paramcheck.annotation.notnull; import com.lzumetal.ssm.paramcheck.annotation.validparam; import org.apache.commons.lang3.stringutils; import org.aspectj.lang.joinpoint; import org.aspectj.lang.annotation.aspect; import org.aspectj.lang.annotation.before; import org.aspectj.lang.reflect.methodsignature; import org.springframework.stereotype.component; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; import java.lang.reflect.field; import java.lang.reflect.parameter; import java.util.arrays; /** * 參數檢查切面類 */ @aspect @component public class paramcheckaspect { @before ( "execution(* com.lzumetal.ssm.paramcheck.controller.*.*(..))" ) public void paramcheck(joinpoint joinpoint) throws exception { //獲取參數對象 object[] args = joinpoint.getargs(); //獲取方法參數 methodsignature signature = (methodsignature) joinpoint.getsignature(); parameter[] parameters = signature.getmethod().getparameters(); for ( int i = 0 ; i < parameters.length; i++) { parameter parameter = parameters[i]; //java自帶基本類型的參數(例如integer、string)的處理方式 if (isprimite(parameter.gettype())) { notnull notnull = parameter.getannotation(notnull. class ); if (notnull != null && args[i] == null ) { throw new runtimeexception(parameter.tostring() + notnull.msg()); } //todo continue ; } /* * 沒有標注@validparam注解,或者是httpservletrequest、httpservletresponse、httpsession時,都不做處理 */ if (parameter.gettype().isassignablefrom(httpservletrequest.class) || parameter.gettype().isassignablefrom(httpsession.class) || parameter.gettype().isassignablefrom(httpservletresponse.class) || parameter.getannotation(validparam.class) == null) { continue; } class<?> paramclazz = parameter.gettype(); //獲取類型所對應的參數對象,實際項目中controller中的接口不會傳兩個相同的自定義類型的參數,所以此處直接使用findfirst() object arg = arrays.stream(args).filter(ar -> paramclazz.isassignablefrom(ar.getclass())).findfirst().get(); //得到參數的所有成員變量 field[] declaredfields = paramclazz.getdeclaredfields(); for (field field : declaredfields) { field.setaccessible(true); //校驗標有@notnull注解的字段 notnull notnull = field.getannotation(notnull.class); if (notnull != null) { object fieldvalue = field.get(arg); if (fieldvalue == null) { throw new runtimeexception(field.getname() + notnull.msg()); } } //校驗標有@notempty注解的字段,notempty只用在string類型上 notempty notempty = field.getannotation(notempty.class); if (notempty != null) { if (!string.class.isassignablefrom(field.gettype())) { throw new runtimeexception("notempty annotation using in a wrong field class"); } string fieldstr = (string) field.get(arg); if (stringutils.isblank(fieldstr)) { throw new runtimeexception(field.getname() + notempty.msg()); } } } } } /** * 判斷是否為基本類型:包括string * @param clazz clazz * @return true:是; false:不是 */ private boolean isprimite( class <?> clazz){ return clazz.isprimitive() || clazz == string. class ; } } |
參數javabean
studentparam.java:
1
2
3
4
5
6
7
8
9
10
11
|
package com.lzumetal.ssm.paramcheck.requestparam; import com.lzumetal.ssm.paramcheck.annotation.notempty; import com.lzumetal.ssm.paramcheck.annotation.notnull; public class studentparam { @notnull private integer id; private integer age; @notempty private string name; //get、set方法省略... } |
驗證參數校驗的controller
testcontroller.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.lzumetal.ssm.paramcheck.controller; import com.google.gson.gson; import com.lzumetal.ssm.paramcheck.annotation.notnull; import com.lzumetal.ssm.paramcheck.annotation.validparam; import com.lzumetal.ssm.paramcheck.requestparam.studentparam; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.responsebody; @controller public class testcontroller { private static gson gson = new gson(); @responsebody @requestmapping (value = "/test" , method = requestmethod.post) public studentparam checkparam( @validparam studentparam param, @notnull integer limit) { system.out.println(gson.tojson(param)); system.out.println(limit); return param; } } |
本節示例代碼已上傳至github:https://github.com/liaosilzu2007/ssm-parent.git
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000014454607