Implementation of an API Service to query on user data which is stored in xml file and mapped to the APIServlet GET method and returning a JSON Array
Application Programming Interface or API is a software intermediary that allows two applications to 'talk' to each other. When you use an application on your mobile phone, this is what happens:
Your task is to implement an API service to query on user data which is stored in com.he.api.data.xml in the following format.
<dataset id_auto_increment="3"›
<user id="1" firstName="John" lastName="Dawson"/>
<user id="2" firstName="John" lastName="Doe"/>
</dataset>
<dataset> tag is the root element in the XML document. It has a single attribute id_auto_increment. The value of Its ID increases as follows: value of the user's ID that was added last + 1
<user /> tag represents a user record in the dataset. It has following attributes:
The call to server would be made by
scheme://domain:port/API?action=#&id=#&firstName=#&lastName=#, where 'string' after '?' denotes the query string.
You have to implement an API service that is mapped to the APIServlet (com.he.api.APIServlet.java) GET Method (at URL scheme://domain:port/API). The query string (read more about query strings here) may have following parameters:
The queries can be of the following types:
Example URL:
scheme://domain:port/API?action=searchById&id=1
Response:
[{
"id": 1,
"firstName": "John",
"lastName": "Dawson"
}l
Example URL:
scheme://domain:port/API?action=searchByFirstName&firstName=John
Response:
[{
"id": 1,
"firstName": "John",
"lastName": "Dawson"
}, {
"id": 2,
"firstName": "John",
"lastName": "Doe"
}]
Example URL:
scheme://domain:port/API?action=searchByLastName&lastName=Doe
Response:
[{
"id": 2,
"firstName": "John",
"lastName": "Doe"
}]
Example URL:
scheme://domain:port/API?action=searchByIdRange&low=1&high=2
Response:
[{
"id": 1,
"firstName": "John",
"lastName": "Dawson"
}, {
"id": 2,
"firstName": "John",
"lastName": "Doe"
}]
Example URL:
scheme://domain:port/API?action=updateUser&id=1&firstName=James&lastName=Jackson
<dataset id auto increment="3">
<user id="1" firstName="James" lastName="Jackson"/>
<user id="2" firstName="John" lastName="Doe"/>
</dataset>
The ID of the user is given by the id_auto_increment parameter of the root dataset tag. After the value is inserted, the id_auto_increment parameter is incremented by 1.
Example URL:
scheme://domain:port/API?action=insertUser&firstName=James&lastName=Jackson
<dataset id auto increment="4">
<user id="1" firstName="John" lastName="Dawson"/>
<user id="2" firstName="John" lastName="Doe"/>
<user id="3" firstName="James" lastName="Jackson"/>
</dataset>
Test your API implementation by comparing the mapping of your JSON array and XML DOM.