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

With the advent of multiple platforms needing to share data and interact with each other restful web apis seems to be all the buzz, building your web projects as web apis allows it to be easily accessible from any form of interface whether web, mobile, things(yes The Internet of things) and infinitely any number of platforms.

Hopefully, this little tutorial will help you get started and be on your way of building your projects more awesome.

WHY USE SPRING BOOT?

Well Well Well, aside from being backed by pivotal, Spring Boot is a framework that really takes the pain out of building enterprise grade applications and let’s you just enjoy building what matters.

SO WHAT ARE WE BUILDING?

So we’re gonna build a really small api to manage movie list, just basic CRUD ( Create, Read, Update, Delete ).

PRE-REQUISITES?

A working knowledge of The Java programming language and Java Persistence API(JPA) would be very nice.  For this tutorial we will make use of the Spring Tool Suite, which is cool ide for developing spring applications.

LET’S GET STARTED

So launch spring tool suite, meanwhile you can find the complete code for this tutorial here.  Okay so go to File->New->Spring Starter Project, set the Name field to the what ever name you want to name your project, I’m naming mine moviecrud, set the Group field to com.project_name ( whatever you named your project).

Click next, type ‘web’ in the dependencies text-box, select “Rest Repositories” , “Jersey”“web”, and “web services”, clear the ‘web’ from the dependencies text-box and type and select “Postgresql”, do the same also for “JPA”.   Click next, then click finish it will download the necessary setup for our little project and do some bootstrapping for us. Isn’t Spring Boot so awesome, no need to worry or have headache in integrating the various parts together, It does it for us.

Let’s examine the MoviecrudApplication.java which you can find under the src/main/java -> com.example.demo.

What i want to point out here is the @SpringBootApplication annotation, that’s the line of code responsible for most of the bootstrapping and a whole lot of the magic that goes on behind the scene, what it basically does is to tell spring to do all that it possibly can to help you in your programming and configuration management including component scanning and the whole host of others.

Create a new package in your project by right clicking com.example.demo and select New -> package, name it com.example.demo.controllers that’s where our controller class will be created, create com.example.demo.data and com.example.demo.repository using the same process, which will contain our entity class and repository class respectively, now you should have four packages com.example.demo, com.example.demo.controllers, com.example.demo.data, com.example.demo.repository. Separating your classes into packages like this helps improve code maintenance.

That’s it for now, we will move on now to part 2 where we will talk a bit about restful architectures and continue with our project.

Have any questions? Do not hesitate to ask in the comments.

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

Leave a Reply

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