SSM Development | parameter checking for SPRINGMVC incoming parameters (using custom AOP facets + custom parameter annotations)

Source: Internet
Author: User

1. Summary

This article is one of the details of the-SSM version of the Little Mall, the project GITHUB:HTTPS://GITHUB.COM/XENV/S-MALL-SSM most of this code can be found in GitHub.

Parameter checking is the legality of verifying whether a user GET or POST passes in a parameter. The simplest method is to use the If check in each controller method, but this is inefficient.

So, is there any other way? SPRINGMVC official recommendation is to use the Hibernate-validate check frame, but it can be said to be very troublesome, but also for each entity, and then write the verifier, the workload is huge.

Here, I use the method of custom AOP facets, combined with custom parameter annotations, it is very convenient to implement a simple parameter verification (here is a non-null check for example), the implementation of the effect is as follows:

    @RequestMapping ("Search")    publicthrows Exception {
}

For the sort parameter, the access may not necessarily come in, we use the @Nullable to mark (this is our definition of annotations), otherwise the default is non-null detection, so that the probability of NULL pointer errors can be greatly reduced.

Some friends may ask, why not use the SPRINGMVC interceptor, but to use the method of AOP, here I draw a diagram for you to explain:

  

If the interceptor is used, the interceptor will intercept before the function call, so that we have no way to do the data validation, and the AOP tangency point can be intercepted after the function call, the function body call, then we use AOP to do the check is very appropriate.

2. Specific implementation 1. Define parameter annotations (I've only got a Nullable here, but the other one doesn't add this default non-empty)
@Retention (retentionpolicy.runtime) @Target ({PARAMETER})  public @Interface  Nullable {}
2. Configuring AOP

Specific do not unfold, many online, maven two dependencies, spring and SPRINGMVC are to add configuration, but also add XML domain, the basic configuration is good

3. Writing AOP Facets

Verificationaspect.java

@Aspect @component Public classVerificationaspect {/*** Declares that the slice is applied to, all controllers, and all public methods in the class that end with the controller*/@Pointcut ("Execution (Public * tmall.controller.*.*controller.* (..))")     Public voidJoinpointinallcontroller () {}/*** method before pointcut execution * *@paramPoint Pointcut*/@Before ("Joinpointinallcontroller ()")     Public voidCheckparameter (Joinpoint point)throwsparameterexception {//get cut-in method parametersobject[] args =Point.getargs (); //how to get into the cutMethod method =( (Methodsignature) point.getsignature ()). GetMethod (); //Get all Parametersparameter[] Parameters =method.getparameters (); //Save the args that need validationArraylist<object> argswithoutnullable =NewArraylist<>(); //non-null validation of parameters without nullable annotations         for(inti = 0; i < parameters.length; i++) {Parameter Parameter=Parameters[i]; annotation[] Annotations= Parameter.getdeclaredannotationsbytype (Nullable.class); if(Annotations.length < 1) {Argswithoutnullable.add (args[i]); }        }         for(Object o:argswithoutnullable) {if(O = =NULL) {                Throw NewParameterexception ("Illegal request, parameter not complete"); }        }    }}

OK, it's actually very simple.

SSM Development | parameter checking for SPRINGMVC incoming parameters (using custom AOP facets + custom parameter annotations)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.