Chapter 25. Code Engineering

  1. Home
  2. Docs
  3. Chapter 25. Code Engineering
  4. 8. Rest API Design and Generation
  5. Using REST API as a service consumer

Using REST API as a service consumer

Download PDF

Consumers of RESTful service have to go through a series of steps in order to obtain the API code required for accessing a REST Resource. This page will show you the steps in detail.

To obtain and use the REST API code as a service consumer:

  1. Visit the API documentation of the service published by service provider. The API documentation should look like this:
    REST API documentation
  2. You can learn the usage of the REST resource by reading through the API documentation. To obtain the API code, scroll down to the bottom of the API documentation. Click on the download link of the REST API model XML file at the bottom of the page.
    Download REST API model XML
  3. Download Visual Paradigm. Install and run it.
  4. Start Visual Paradigm.
  5. Import the REST API model XML file into Visual Paradigm by selecting Project > Import > XML… from the toolbar.
  6. In the Import XML window, enter the file path of the XML file and click Import.
    The Import XML window
  7. In the Diagrams tab of the Project Browser, double click on the class diagram created by importing the XML file.
    Open the class diagram
  8. You can now see the communication model of the REST Resoure which looks like this:
    The communication model
  9. Now, you can generate the API code. Select Tools > Code > Generate REST API… from the toolbar.
  10. In the REST API window, select Consumer as API Type.
    Select Consumer to be API Type
  11. Select the REST Resource to generate code.
    Select the REST Resource to be generated
  12. Skip the Company field as you don’t really need it in programming.
  13. Enter the base URL of the service.
  14. Check Generate Sample to generate the source code that teaches you how to access the service. The sample code is rich and informative. Therefore, instead of programming from scratch, we strongly recommend you to generate the sample code and modify its content to fit your needs.
  15. Enter the output path of the code.
    Output path entered
  16. Click Generate. Depending on the option checked/unchecked, you may see the following folders in the output directory.
    Folder Description
    lib In order for the generated code to work, the Google Gson library must be presented in your class path. To avoid any compatibility issues, please download library manually:
    https://code.google.com/p/google-gson/and then place the file in the lib folder.
    sample_src The sample code that shows you how to access the service. We strongly recommend you copy the code and modify it by filling in your own application logic.
    src The source code of the communication model. Do not modify the file content or else the code may not function properly.