In this assignment, I will explain how my project will run but before that I assume that Docker, H2 Database and Netbeans are installed in the system.
-
Download the Code from the GitHub and import the same in your respective IDE.
-
Run the UrlProjectApplication inside the com.url.shortner package which will initiate our Spring Container and start our tomcat server on port 8080.
-
Now, from the output screen copy the connection url to establish a connection with our database (H2 Database).Example: jdbc:h2:mem:a6f820b6-0fc0-4872-8646-81a184136a82.
-
Make sure you have installed H2 as when you copy the JDBC url after that go to your browser and enter localhost:8080/h2-console.
-
Paste your connection url under JDBC url and click Test Connection once it says Test successful then click Connect.
-
After this, open Postman where we will initiate POST and GET API calls and receive our shortlink.
-
Firstly, In the POST calls enter localhost:8080/generate in the cell and within its body (set to JSON) enter the long url and expiration date(optional) and click Send.
-
The ShortLink along with Original URL and its expiration date will appear in the Response body and the same can be seen in our H2 database by writing Select * from URL and enter Run.
-
For the GET API (change POST to GET) call write localhost:8080/{shortlink} in the cell and we can see the web page in our Response body in Preview Section.
-
After that we build our project by right click on our project and then go to Run Maven -> Goals, a following window appears. (as per Netbeans)
-
In the Goals section write mvn clean install to build and get our target folder which will be used to make our dockerfile.
-
The following commands were used in the command prompt(docker installed) to build the docker file.
• Go to the respective folder/directory where the project is kept and run docker build . command. • Get the Image ID using docker image ls. Then, run the command docker run -p 8081:8080 -d {imageID} to get our running container at port 8081. • Then you can generate the POST request by writing localhost:8081/generate in Postman to get the short link and writing the same to the browser as localhost:8081/{shortlink}.