How to Use the Generated REST API?
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:
- Visit the API documentation of the service published by service provider. The API documentation should look like this:
REST API documentation
- 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
- Download Visual Paradigm. Install and run it.
- Start Visual Paradigm.
- Import the REST API model XML file into Visual Paradigm by selecting Project > Import > XML... from the toolbar.
- In the Import XML window, enter the file path of the XML file and click Import.
The Import XML window
- In the Diagrams tab of the Project Browser, double click on the class diagram created by importing the XML file.
Open the class diagram
- You can now see the communication model of the REST Resoure which looks like this:
The communication model
- Now, you can generate the API code. Select Tools > Code > Generate REST API... from the toolbar.
- In the REST API window, select Consumer as API Type.
Select Consumer to be API Type
- Select the REST Resource to generate code.
Select the REST Resource to be generated
- Skip the Company field as you don't really need it in programming.
- Enter the base URL of the service.
- 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.
- Enter the output path of the code.
Output path entered
- 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:
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. Description of generated files
The following resources may help you to learn more about the topic discussed in this page.
|3. Generating REST API as Service Provider||Table of Contents||Part X. Database design and engineering|