Building Skeleton Rest API with Spring Boot, Kafka, Postgres

Stevano Candra
DOKU Insight
Published in
7 min readNov 12, 2018

I just want to share about my skeleton project that using all above in it. I know that my programming not so good and not to good but feel free to reach me if somehow you kinda have a question. Hopefully I can answers your questions. (This is my 1st article that I wrote online, so sorry if I made an mistakes at writing it 😅 )

What you need ? (Skip this steps if you familiar)

First you need IDE that support java either IntelliJ, Eclipse, Netbeans, etc. Second install Postgres, Kafka on your local machine. Third lets begin coding.

I personally enjoy using IntelliJ so this tutorial based on that.

Choose File > New > Project … > Spring Initializr (Figure 1)

Figure 1

After you open looks like window above you are correct after that press next.

Figure 2

Change all project metadata so its look like Figure 2. After that press Next. Just Next until you find Finish button. (we configure it manually 😄 )

For those who not using IntelliJ can visit to https://start.spring.io don’t forget to change your group and artifact. Same it with Figure 2 and Press Generate Project. After that just open with your desire IDE.

Now let’s begin !

Open your project / if you already open it look for pom.xml we have to modified like below xml.

Take a look at line 33 until 100 it’s our dependencies, you can remove that you only need.

Before we continue to code take a look at application.properties that in resources [src > main > resources > application.properties]

For line 1 and 2 is the name of our application and open port. At line 3 until end is our Postgres configuration with databases name ‘inventories’. In this skeleton I don’t show how to configure database at Postgres my assumption you all already know how but I included the database schema below.

Okey now we take a look at our main SpringBoot application yours should take a look like this

From line 1 until 15 is default SpringBoot main generate by our IDE. From line 17 until 23 is E-Tag supporting, this code is handling cache in our Rest API and you should added cache.xml in our resources directory with like down below.

Above xml is supporting cache that request by our users. If you curious and want detail about E-Tag you can read at

Next we move to create basic authenticate regarding security and safety when we deploy our application.

Create java file under our package [src > main > java > com.inventories] with name AuthenticationEntryPoint.java and added code like below.

Above code is to handling if authentication failed and return unauthorized. Before added new java file take a look to our application.properties and make some adjustment like below.

And next create new java file with name SpringSecurityConfig.java with code like below.

We added our authentication entry point here and added property source to get some configuration from our application properties for user authentication. Above is how to set authorize the request using http security, only users that has authorization as admin that can access all of the url ‘/api’ and ‘/admin’ for the standard user only can access ‘/api’. The setting at line 22 until 31. From line 33 until 43 is how we configure our credential properties and load it to in memory authentication. In this skeleton I’m using Basic Authentication that using encoding there are another Digest Authentication you can search how it works I’m not explain it in this article 😅.

Next, we create new package under com.inventories name with model. It for our entity model. There are 2 models; BrandEntity and BrandManufacturerEntity below is the code.

Then we create new package with name repo under com.inventories and create java file with name BrandRepo.java and BrandManufacturer.java like below

Because I’m using Postgres so at line 14 and 15 at BrandRepo.java I had to use native query to get next sequence ID if you using different databases maybe you have to make some adjustment at the codes above.

Next create package with name service and added with codes below.

Above is our service that sign by @service to implement all the function at our repository such as add, update, find brand get all data.

Next, create package with name kafka under com.inventories. And create new Java file with name KafkaProducer.java and match all the code below.

From above code in this producer handling 2 services 1 for brand and 1 for brand manufacturer with different topic but same group and the model I’m mapping to string so that can be store to Kafka but there are some other way that we can store as binary but I find it that the easiest is store as string. Next we create consumer name it with KafkaConsumer.java.

For KafkaConsumer above is the consume code that will listen when Kafka is sending the String with some topic take a look I have different topic for each services there are patch, update, add, etc this will call method or function at our services that are BrandService and BrandManufacturerService.

For depth understanding consumer and producer please read my reference

At above article you can find how to running Kafka at your local machine and best practices for code. At this skeleton is my assumption to using Kafka this isn’t the best practices but it is working 😅.

Don’t forget adjust our properties file append below code.

And next is our SpringBoot Controller there are 2 controller brand and brand manufacturer. Create BrandController.java and BrandManufacturerController.java under our package com.inventories.controller

For line 30 is the request mapping or our request url for brand resources that next we can apply get, post, put, patch. For line 34 – 41 is our SpringBoot Auto wired that connecting this controller to Brand Service, Kafka Producer and Kafka Consumer. As for 43 – 53 is linked to our properties file.

When the hit our url at “/api/brand” with method get so it will into the getAllByBrandName (line 2) function and at there we accept parameter such as page, size, sort, etc. If you want each page is 12 data, you can put it at size parameter 12 (integer) this function will return our brand in databases and return it as Resources if you put header-accept as ‘application/xml’ it will return xml if you put ‘application/json’ it will return json and much more. And from line 22 is when we hit our url at “/api/brand/1” it will return brand with specific id that find at Kafka first (line 26) if not query it to database. It will return same as our function at line 2 based on header-accept.

Code above is how we added new Brand to our DB there is 2 url one with Kafka and not. Both return same responses the demo below.

Next, we create new java file name it BrandManufacturerController.java and make it look like below code.

Code above is only can be access by user that has role as “admin” and it link to AuthenticationEntryPoint.java from function getAllBrandManufacturer it will get all brand and without paging like at our BrandController that has pages parameter.

This below is the demo for the working application.

So this is the end of this article. I know that this application ain’t perfect but you can perfect it and all above isn’t the best practice but it working 😃

If you want to learn it more you can clone it from my GitHub

https://github.com/stevanocandra/rest-api

--

--

Stevano Candra
DOKU Insight

Scrum Master | Web Developer | Gaming Enthusiast | from Indonesia, Jakarta | https://www.linkedin.com/in/stevanocandra/