why:
為什么要用aop實(shí)現(xiàn)校驗(yàn)?
answer:
spring mvc 默認(rèn)自帶的校驗(yàn)機(jī)制 @valid + bindingresult, 但這種默認(rèn)實(shí)現(xiàn)都得在controller方法的中去接收bindingresult,從而進(jìn)行校驗(yàn).
eg:
1
2
3
4
5
6
7
|
if (result.haserrors()) { list<objecterror> allerrors = result.getallerrors(); list<string> errorlists = new arraylist<>(); for (objecterror objecterror : allerrors) { errorlists.add(objecterror.getdefaultmessage()); } } |
獲取errorlists。這樣實(shí)現(xiàn)的話,每個(gè)需要校驗(yàn)的方法都得重復(fù)調(diào)用,即使封裝也是。
可能上面那么說(shuō)還不能表明spring 的@valid + bindingresult實(shí)現(xiàn),我先舉個(gè)“栗子”。
1. 栗子(舊版本)
1.1 接口層(idal)
eg: 簡(jiǎn)單的post請(qǐng)求,@requestbody接收請(qǐng)求數(shù)據(jù),@valid + bindingresult進(jìn)行校驗(yàn)
- httpmethid: post
- parameters:@requestbody接收請(qǐng)求數(shù)據(jù)
- valid:@valid +bindingresult
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@responsebody @postmapping ( "body" ) public responsevo bodypost( @requestbody @valid testvo body,bindingresult result){ //校驗(yàn)到錯(cuò)誤 if (result.haserrors()) { list<objecterror> allerrors = result.getallerrors(); list<string> lists = new arraylist<>(); for (objecterror objecterror : allerrors) { lists.add(objecterror.getdefaultmessage()); } return new responsevo(httpstatus.bad_request.value(), "parameter empty" , lists); } return new responsevo(httpstatus.ok.value(), "bodypost" , null ); } |
1.2 實(shí)體(vo)校驗(yàn)內(nèi)容
@valid + bindingresult的校驗(yàn)注解一大堆,網(wǎng)上一摸就有的!
1
2
3
4
5
6
7
8
9
10
|
public class testvo { @getter @setter @min (value = 0 ,message = "請(qǐng)求參數(shù)isstring不能小于0" ) private integer isint; @getter @setter @notblank (message = "請(qǐng)求參數(shù)isstring不能為空" ) private string isstring; } |
1.3 結(jié)果測(cè)試
2. aop校驗(yàn)(升級(jí)版)
可以看到若是多個(gè)像bodypost一樣都需要對(duì)body進(jìn)行校驗(yàn)的話,那么有一坨代碼就必須不斷復(fù)現(xiàn),即使改為父類可復(fù)用方法,也得去調(diào)用。所以左思右想還是覺(jué)得不優(yōu)雅。所以有了aop進(jìn)行切面校驗(yàn)。
2.1 接口層(idal)
是的!你沒(méi)看錯(cuò),上面那一坨代碼沒(méi)了,也不需要調(diào)用父類的的共用方法。就單單一個(gè)注解就完事了:@paramvalid
1
2
3
4
5
6
|
@paramvalid @responsebody @postmapping ( "body" ) public responsevo bodypost( @requestbody @valid testvo body,bindingresult result){ return new responsevo( "bodypost" , null ); } |
2.2 自定義注解(annotation)
這個(gè)注解也是簡(jiǎn)簡(jiǎn)單單的用于方法的注解。
1
2
3
|
@target (elementtype.method) @retention (retentionpolicy.runtime) public @interface paramvalid {} |
2.3 重點(diǎn)!切面實(shí)現(xiàn)(aspect)
切面詳解:
@before: 使用注解方式@annotation(xx),凡是使用到所需切的注解(@paramvalid),都會(huì)調(diào)用該方法
joinpoint: 通過(guò)joinpoint獲取方法的參數(shù),以此獲取bindingresult所校驗(yàn)到的內(nèi)容
遷移校驗(yàn)封裝: 將原先那一坨校驗(yàn)遷移到aspect中:validrequestparams
響應(yīng)校驗(yàn)結(jié)果:
- 通過(guò)requestcontextholder獲取response
- 獲取響應(yīng)outputstream
- 將bindingresult封裝響應(yīng)
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
|
@aspect @component public class paramvalidaspect { private static final logger log = loggerfactory.getlogger(paramvalidaspect. class ); @before ( "@annotation(paramvalid)" ) public void paramvalid(joinpoint point, paramvalid paramvalid) { object[] paramobj = point.getargs(); if (paramobj.length > 0 ) { if (paramobj[ 1 ] instanceof bindingresult) { bindingresult result = (bindingresult) paramobj[ 1 ]; responsevo errormap = this .validrequestparams(result); if (errormap != null ) { servletrequestattributes res = (servletrequestattributes) requestcontextholder.getrequestattributes(); httpservletresponse response = res.getresponse(); response.setcharacterencoding( "utf-8" ); response.setcontenttype(mediatype.application_json_utf8_value); response.setstatus(httpstatus.bad_request.value()); outputstream output = null ; try { output = response.getoutputstream(); errormap.setcode( null ); string error = new gson().tojson(errormap); log.info( "aop 檢測(cè)到參數(shù)不規(guī)范" + error); output.write(error.getbytes( "utf-8" )); } catch (ioexception e) { log.error(e.getmessage()); } finally { try { if (output != null ) { output.close(); } } catch (ioexception e) { log.error(e.getmessage()); } } } } } } /** * 校驗(yàn) */ private responsevo validrequestparams(bindingresult result) { if (result.haserrors()) { list<objecterror> allerrors = result.getallerrors(); list<string> lists = new arraylist<>(); for (objecterror objecterror : allerrors) { lists.add(objecterror.getdefaultmessage()); } return new responsevo(httpstatus.bad_request.value(), "parameter empty" , lists); } return null ; } } |
2.4 測(cè)試結(jié)果
看了上面兩種結(jié)果,就可以對(duì)比出使用spring aop 配合@valid + bindingresult進(jìn)行校驗(yàn)的優(yōu)點(diǎn):
- 去除代碼冗余
- aop異步處理
- 優(yōu)化代碼實(shí)現(xiàn)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://juejin.im/post/5a5e1159518825732b19d8ce