Customize existing REST handlers

We can customize existing REST request handlers using a post-process command.


About this task

We can inject the custom post-process command for the existing REST command using the @ResourceHandlerPostprocessAnnotation annotation.


Procedure

  1. Create a custom command interface that has @ResourceHandlerPostprocessAnnotation that is defined as follows.

      @ResourceHandlerPostprocessAnnotation(
      path="XXX",
      method="GET",
      query="param=value",
      accessCheck=true)
      public interface CustomPostCommand {
      ...
      }

    Where

      path
      Required. Specifies the REST API path. Specifies REST API path. The relevant part of the path is the string that follows storeId in the rest API URI.

      method
      Required. Specifies the HTTP method to be used, which is either GET, POST, PUT, or DELETE.

      query
      Optional. Specify a REST QPI query string after the path. For instance, if a REST API short path is self/usable_billing_address/{orderId}?q=nameXXX, we can add the REST query into ResourceHandlerPostprocess using the parameter query="q=nameXXX".

      accessCheck
      Optional. Specify if access control is needed for the post-process controller command. If true, then the access control check is required. In all other cases, the check is not performed. The default value is false.

  2. Create a custom command implementation class, which extends the ControllerCommandImpl and implements the new created custom interface.

      public class CustomPostCommandImpl extends ControllerCommandImpl implements CustomPostCommand {
      // Add the logic to get more customized info
      // Add the customized info into responseProperties
      // We can get request param by calling requestProperties.get("request")
      }

  3. Restart the server and test your post-process command by calling the corresponding rest API.


Example

Following are two sets of sample code. Both examples show how to use the request parameters and the original response from the extended REST APIs. Example A:

Example B: