(11) Interceptor

Source: Internet
Author: User
Tags json

EasyOpen started to support interceptors in version 1.3.1.

The EasyOpen Interceptor implementation principle is similar to the Springmvc Interceptor, where the interceptor acts on the API method, that is, the @api annotation method.

The interceptor is defined as follows:

/** * Interceptor, principle with SPRINGMVC interceptor * @author TANGHC * */public interface Apiinterceptor {/** * preprocessing callback method, executed before method call * @param Request * @param response * @param serviceobj service class * @param Argu method Parameters * @return * Throws Exception */boolean Prehandle (HttpServletRequest request, httpservletresponse response, Object serviceobj

    , Object Argu) throws Exception;
     This method is called after the/** * interface method finishes executing. * @param Request * @param response * @param serviceobj service class * @param argu parameter * @param The result method returns a knot Fruit * @throws Exception */void Posthandle (HttpServletRequest request, httpservletresponse response, Object SE

    Rviceobj, Object Argu, object result) throws Exception;
     /** * Results after package execution * @param request * @param response * @param serviceobj service class * @param argu parameters * @param result is wrapped * @param e * @throws Exception */void Aftercompletion (HttpservLetrequest request, HttpServletResponse Response, Object Serviceobj, Object Argu, object result, Exception e)

    Throws Exception;
/** * Match Interceptor * @param Apimeta Interface Information * @return */boolean match (Apimeta Apimeta); }

An adapter Apiinterceptoradapter Interceptor execution process is also provided:

Same as the SPRINGMVC interceptor execution Process Prehandle If False is returned, the API method is not called, and then aftercompletion is called in reverse order, which needs to be returned by response custom if Prehandle returns True. Proceed to the next Prehandle prehandle execution is complete, reverse execution posthandle

The final reverse call aftercompletion the normal flow:


apimethod.invoke ()//API method call


Configuring Interceptors

Create a new log processing interceptor, inherit the Apiinterceptoradapter, override the method in the parent class

public class Loginterceptor extends Apiinterceptoradapter {@Override public boolean prehandle (httpservletrequest Request, HttpServletResponse response, Object Serviceobj, Object Argu) throws Exception {System.out
        . println ("======prehandle======");
        System.out.println ("IP:" + requestutil.getclientip (request));
        SYSTEM.OUT.PRINTLN ("interface class:" + Serviceobj.getclass (). GetName ());
        if (Argu! = null) {SYSTEM.OUT.PRINTLN ("parameter class:" + Argu.getclass (). GetName ());
    } return true; } @Override public void Posthandle (HttpServletRequest request, httpservletresponse response, Object serviceobj, O
        Bject Argu, Object result) throws Exception {System.out.println ("======posthandle======");
        SYSTEM.OUT.PRINTLN ("interface class:" + Serviceobj.getclass (). GetName ());
        if (Argu! = null) {SYSTEM.OUT.PRINTLN ("parameter class:" + Argu.getclass (). GetName ()); } System.out.println ("Result:" + JSON.)toJSONString (result)); } @Override public void aftercompletion (HttpServletRequest request, httpservletresponse response, Object Serviceo BJ, Object Argu, object result, Exception e) throws Exception {System.out.println ("======aftercomplet
        ion====== ");
        SYSTEM.OUT.PRINTLN ("interface class:" + Serviceobj.getclass (). GetName ());
        SYSTEM.OUT.PRINTLN ("parameter class:" + Argu.getclass (). GetName ());
        SYSTEM.OUT.PRINTLN ("End result:" + json.tojsonstring (result));
    System.out.println ("E:" + e); }


To add interceptors to the APICONFGI:

    protected void Initapiconfig (Apiconfig apiconfig) {

        Configure Interceptor
        apiconfig.setinterceptors (
                new apiinterceptor[] {new Loginterceptor ()});

Intercept Range

By default, all interfaces are blocked, and if you want to intercept the specified interface, you can override the Boolean match () method:

Block only the Goods.get interface
@Override public
    boolean match (Apimeta Apimeta) {
        return apimeta.getname (). Equals (" Goods.get ");

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.