A REST service build to retrieve list of cities stored in a database. The service uses the following technologies:
- Java
- RESTFull service
- Data manipulation layer
- Spring 4 and Spring-boot
- Maven
- MySQL and H2 (in memory) database
- Testng for Testing
This implementation provides a GET/POST REST service to retrieve the list of cities in the database, and return them as a JSON object.
The service is idempotent
in inserting new entries, that is, if a country (or city-country combination) is inserted again, the database will maintain only a single copy of the data.
-
GET list all the cities
Returns: list of cities present in the database.
Optional [country]: If present, filters the city list on the bases of specified country name. It searches (case-insensitive search) on part of country name as well.
-
GET load sample data present at
src/java/resources/SampleData.csv
Returns: the list of cities uploaded from the file.
JSON Output format [for both GET calls]:
[
{
"id":86,
"name":"New York",
"country":{
"id":2,
"name":"United States"
}
},
{
"id":87,
"name":"Los Angeles",
"country":{
"id":2,
"name":"United States"
}
},
{
"id":88,
"name":"Atlanta",
"country":{
"id":2,
"name":"United States"
}
}
]
-
POST add user provided cities (and countries) into the database
Can use any REST client like POSTMAN
to send POST request
Content-Type : application/json
[
{
"name":"Mumbai",
"country":{
"name":"India"
}
},
{
"name":"Bangalore",
"country":{
"name":"India"
}
}
]
Returns: status of the operation HTTP 200 OK
- Open terminal and
cd
to the directory where you want to clone the project. - Clone the project using:
git clone https://github.com/jain7aman/spring-jpa-jersey.git
cd spring-jpa-jersey
- Run:
mvn clean install
cd target
and copy therest.war
file.- Paste the war into
webapps
directory of Apache Tomcat - Start the Tomcat server by going into the bin directory and execute startup.bat (for Windows) or startup.sh (for Linux) file.
Note: Can also run mvn verify
to run the test cases.
-
The solution supports MySQL as well as H2 in-memory database. To change the database configurations, edit:
-
application.properties
underspring-jpa-jersey/src/java/resources
for main project
-application.properties
underspring-jpa-jersey/src/test/resources
for test cases -
Database schema and tables will be created automatically and the data persists between service invocations. To start with the empty tables every time you deploy the service, change value of
hibernate.hbm2ddl.auto
parameter (inapplication.properties
file for main project) tocreate-drop
orcreate
.