引言
使用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