Create simple REST API for practice automation testing
Tags: rest, api, java, spring
Published: 2019-09-23 01:25:17

---
title: Create simple REST API for practice automation testing
published: true
description: 
tags: #REST #API #Java #Spring
---

API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications (services) to talk to each other.

Imagine it like a waiter/waitress in a restaurant. It will help you get your info about your order and transfer it to the cooks in the kitchen. When the food is ready, it will go get the food and bring it to you.

In tech terms, the database is like a kitchen. API is like a waiter. And you(the customer) is like client request.

To create APIs, we can do it in many ways/languages. But for this tutorial I will show you how to do it with Java Spring/Tomcat with mvn.

The database we interact with will be MySQL.

1.Install MySQL and create a new database:

MySQL is an opensource RDMBS so feel free to install and use.
As I'm using Ubuntu, the installation guide you can refer to [MySQL installation](https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-18-04)


After that, you can create your own database, which in this blog post, name as gamelistapi.

Refer to this for how to create a database in MySQL [Create a database] (https://www.a2hosting.com/kb/developer-corner/mysql/managing-mysql-databases-and-users-from-the-command-line).

To work with databases, I often use DataGrip ( a development tool created by Jetbrains). DataGrip support a lot of databases : MySQL, Oracle, SQL server, Postgres, MariaDB, etc..

Datagrip official site is [here][https://www.jetbrains.com/datagrip/]

After create your new database, you can open Datagrip to interact with your database via SQL query or by Datagrip GUI.

Create new connection:
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/7m8kg6r5kjkdggsd9j7q.png)


Config for connect with gamelist api:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/f2lrkl8veghwcu1rrv2w.png)

2.Setup Java and Maven Environment:

You will need to install Java (I'm currently using Java 11 but you can go with Java 8 as it is stable version).

Go to oracle site for download the proper java installer for your system: [Java oracle](https://www.oracle.com/technetwork/java/javase/downloads/index.html)

After that install maven tool (used for building java app). Please go to maven download official site for download maven : [maven latest version](https://maven.apache.org/download.cgi)

You will need to setup global environment for java and maven environment before you can use java and maven in your project. (The setup depends on which OS you are using)

3.Project structure:

- In maven project we will have pom file: to store dependency (library) we will use for our project)

- In src/resources folder, we usually put in the database, environment, configuration information used for the project. In this project, we put db.properties.

The file db.properties looks like this :

````javascript

// MySQL properties

mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/gamelistapi?useSSL=false
mysql.user=****
mysql.password=*****

// Hibernate properties

hibernate.show_sql=true
hibernate.hbm2ddl.auto=update


// C3P0 properties

hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.acquire_increment=1
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=150


```

mysql.driver is the param we would use for jdbc driver for MySQL.
mysql.url is the url for our local database. (Usually MySQL will take the port 3306)
mysql.user and mysq.password you will put your user and password of your created database in there.

- In src/main packages, you will have source code that implement the gamelist api.

I will cover about structure of Spring framework in another blog post.

- Features of gamelist api will have: create new game, update game, get game data, and delete the game.

4.Build gamelist api with maven:

Usually to run project from maven, you will run with 'mvn ...'

Since our project use tomcat7, the command line will be:

mvn tomcat7:run

5.Using gamelist api with postman:

Postman is a free tool for users interact with API.
You can download postman from this link: [postman](https://www.getpostman.com/downloads/)

For example, if you want to get all the games from the API, the screenshot looks like this.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/swz1w88uc326677x702h.png)

If you want to create new game:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/lyvenzqdjvmycx2kvfc0.png)


Feel free to get the code from my github [gamelist-api](https://github.com/cuongld2/gameListApi)

Next blog post, I will show you about how to test these APIs with Serenity.

Thank you all for reading! :-*

Notes: If you feel this blog help you and want to show the appreciation, feel free to drop by :

[<img src="https://thepracticaldev.s3.amazonaws.com/i/cno42wb8aik6o9ek1f89.png">](https://www.buymeacoffee.com/dOaeSPv
)

This will help me to contributing more valued contents.