How to build restful web apis in spring boot and deploy it to heroku? Part-2

Before we speed ahead let’s have a quick chat about restful architecture. When we say an application is restful, what do we mean? in a simple and watered down form what this mean is that the application treats and handles data being exchanged as resources, this brings us to a very important topic

Formation Of URL.

Url’s are one of the fundamental components that define if an application is restful or not.  For the most part, in developing restful web applications think of Url’s in terms of noun phrases and not verbs, for example let’s say we want to create a new movie using a url like /movie/create (‘create’ is a verb)  is not considered restful at all, the correct way would be to send an HTTP POST request to a url in the format of  /movies.  For more information and deeper understanding of Restful architectures do check-out this tutorial.

Let’s Get Back Into Our code

We’ll create a new entity class called Movie in the package, our class will have id, movieName and movieDescription variables, we’ll also create the corresponding getter and setter methods for our variables, you should have some thing like this

Also let’s create our JPARepository interface which will help us with CRUD functionalities with our entity, so we’ll create an interface called MovieRepository that extends JpaRepository in the com.example.demo.repository package.  You should have something that looks like this:

Alright let’s continue,  now we’ll create our controller, This is the component that will handle requests coming from the outside, our controller will be created in the com.example.demo.controllers package.  The code will look pretty much like this:

Let’s talk a bit about the code above,  in line 18, you see an anotation called @RestController, what this practically tells spring boot is that, “hey I’m building this controller to handle restful api request so configure all you can configure to help me get started with it”, the @RequestMapping annotation we see in line 28 practically says “let this method that this annotation is applied on top handle all Http GET request to the path /movies”,  the same concept is repeated in other lines also.

Before we test our code, let’s create our database which will be on postgresql in this case, if you haven’t used postgresql before feel free to substitute it with whatever database you are familiar with, so we’ll create a database called moviecruddb ( a quick note when using postgresql let all things be in small letters).  There’s one more thing we need to do, after creating our database we go into the src/main/resources-> and configure our database connection there, we end up having something like this:

so yes that’s pretty much it, hit the run button and let’s test our rest api :).  To test our api we need to use something called an api client, pretty much because a browser isn’t sufficient enough, for this i kinda always go with postman, so I’ll fire up postman and make a HTTP POST request to /movies to create a new movie entry with the body:

and header ‘content-type: application/json’.  Test other routes and let me know in the comments if you had any issue.

Next we’ll go on to deploy our small api on heroku, you can get all the code here on github

One comment on “How to build restful web apis in spring boot and deploy it to heroku? Part-2

Leave a Reply

Your email address will not be published. Required fields are marked *