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
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.
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.
Config for connect with gamelist api:
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
After that install maven tool (used for building java app). Please go to maven download official site for download maven : maven latest version
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)
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 :
// 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:
5.Using gamelist api with postman:
Postman is a free tool for users interact with API.
You can download postman from this link: postman
For example, if you want to get all the games from the API, the screenshot looks like this.
If you want to create new game:
Feel free to get the code from my github gamelist-api
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 :
This will help me to contributing more valued contents.