ART#402 - Running your first ATG REST MVC service



In the previous article we have discussed the basics of what exactly is a REST Service, what does ATG has to offer in relation to REST and how to enable REST services in your ATG application.
If you are unclear about these topics, it is highly recommended to go back to ART#401 - ATG Rest MVC Basics and study the basics before starting with this article.

Let's Begin.

Rest Services and URLs
We run REST services based on URLs. How? Let's see.
Suppose you have a fully functional eCommerce website. Now, you are creating a mobile application (on Android or iOS) and your task is to display order details when a user enters the orderId and submits the form.
If you were to do this on a website, the process would be simple - submit the form via GET or POST method. Your orderId goes to the server, your orderId is checked if it is valid or not. If it is valid - order details are displayed in a JSP and rendered as an HTML page on a browser.
Now, if we were to do the exact same thing for a mobile application using REST, what should we do? The process is very much similar.
1. You create a form on the mobile application
2. At the server level, you HOST A URL which takes orderId as input and returns the response in JSON or XML format.
(At this point, let's not worry about how you "HOST A URL", we will discuss this later.)
3. The mobile application hits the hosted URL (via GET/POST method - whichever applicable) along with the orderId parameter
4. The server accepts the request, processes it and returns a response in XML/JSON format (configured at server level). We will also discuss how to change response format (XML/JSON) in later sections.

Example of a GET URL:
orderId goes as a part of URL

Example of a POST URL:
orderId (ord123456) goes as a parameter in the body.

This pretty much clears up that REST services (ATG or other platforms) work on URLs. The process of making these URLs, hosting them, implementing their functionality - we will discuss all of these in later sections.

Out of the BOX URLs
Just like we have out of the box functionality/API in ATG, we have OOTB REST URLs corresponding to these functionalities (Add to cart, remove from cart etc.) which can be used as-is. 
Please note that these OOTB URLs will work only if you have included the module "REST" while building your EAR.

Running your first OOTB Rest Service
Firstly, for making a REST call you can do so via any one the following:

1. Your browser (only for GET Requests)
2. Advanced REST Client (you can download this application or chrome extension)
3. Postman (you can download this application or chrome extension)
4. curl command using command prompt (a little complicated for beginners)
We will use Postman in these tutorials, therefore I recommend you install it.

Let us take a very basic example of an OOTB Rest URL (of GET type).

The response which it produces is something like:


In postman, it will be something like:

1. You can choose the request type (red box on the left) - GET/POST/any other type.
2. Put-in the hosted/OOTB URL
3. Click on "SEND" button
This will hit the URL, receive a response from the server and display it.

Similarly, a post request can also be sent with parameters using Postman:

1. Change the request type to POST.
2. Put-in the hosted/OOTB URL
3. Click on "Body"
4. Select the "x-www-form-urlencoded" radio button
5. Put in the parameter names and values (in key and value textboxes respectively)
6. Click on SEND.
So this is how we use and test the REST URLs.

No comments:

Post a Comment


Get All The Latest Updates Delivered Straight Into Your Inbox For Free!