REST-API custom service name and interceptors

Hi,

I have a few questions on REST-API addon.

  1. Can I define my own service name instead of /rest/v2/services/_RestService/…
    e.g. /rest/v2/services/myapi ?

  2. Can I define Spring interceptors to the REST API calls so that we can run some logic before the call happens ?
    a) To limit the number of calls (previous example given was not using REST-API addon)
    b) To control which user can call a specified REST API endpoint
    c) To log the API call for analytics and billing

CK

Hi,

  1. The REST API add-on provides generic api, so naming rules are generated and not flexible. Invocation path is defined by the service bean name. So, if you want to alter it, you may consider creating custom REST controllers and delegate service invocations to them. Another option - request rerouting using reverse proxy like nginx.

  2. As for the interceptors for the generic REST API - yes, it is doable. I have extended the previous example: added a HelloService that is invoked from the custom REST controller and via Generic REST configuration. The PoC’s source code is attached. I’ve added com.company.untitled.web.interceptors.GenericRestRateLimitInterceptor class and use it for the generic rest invocations, see com\company\untitled\rest-dispatcher-spring.xml file. In the interceptors you can limit invocation rate and log api calls, but as for the security - you may consider documentation on creating a custom secured endpoint here

Please let me know if you have more questions.

untitled.zip (96.7 KB)

There is a project on GitHub now.

2 Likes