My Experience with Developing API using Java Spring Boot| Coding.

Learning Java in an unusual way.

Apoorv
Mindroast
6 min readAug 29, 2021

--

Java spring is a famous framework used to build web applications on top of Java EE. Java EE stands for Enterprise edition, which is community-driven enterprise software. Java EE has contributions from industry experts, commercial, community, and open-source contributions. It can be used with any Java application. After years of coding using different scripting languages like JavaScript and Python, I felt the urge to learn the concept of Java once again.

Java Spring Boot

In this tutorial, we will exploring the concept of Java Spring framework by developing a basic rest full service in which we will expose few endpoints related to Camera Management System.

Prerequisite

  • Java
  • Basic understanding of Programming(Any language).
  • Spring Tool Suite / Eclipse, or any editor of your choice.
  • Basic understanding of Rest APIs.
  • Postman( Anything Similar)

This will be moreover a journey in which anyone interested in learning Java can explore it along with me. I don't want to make it as a tutorial series but wanted to design it as a roadmap in which one can make plenty of mistakes, but learn something new every week.

We will be making the project for the CRUD operations for our camera management system. Just to make things more clearer, the agenda is to develop different Rest API that will be used to perform multiple actions on the Camera list. In this particular tutorial we won’t be connecting our application with the database, but will be using in-memory storage.

1. Setting up the basic bootstrap spring project

By visiting the website which is well known for creating basic Java spring bootstrap project. Open the following link https://start.spring.io/, we will be setting our basic project.

  • Group: com.camera-service
  • Artifact: camera
  • Name: camera
  • Packaging: jar
  • Java: 8 (I have installed this version in my system.)

Once you have provided all the information, it the time to download the code into your machine by clicking on Generate button. Once the code in downloaded, unzip and open it your Spring Tool Suite. Simple click on
File > Import > Maven > Existing Maven Projects > then browse to the location where you have unzipped the code.

After selecting your Existing Maven Projects directory, and do select the POM file and then click finish. Great you have successfully cleared path first.

We have a basic skeleton ready, for starting with our Java Camera Rest API Microservice.

2. Designing a controller.

Let’s start with building a basic Controller with four different Rest method, right click on the project and create new Java Class. You should add correct package name and name of your controller.

Once done, add following line to your code. By this we are making controller for 3 apis end point to Create and Read the in memory data.

Notes

@RestController annotation is an alternative of Controller annotation which is introduced in Spring 4.0, this help in a specialization of the Component class.

3. Developing Service Class.

Once the controller is ready, let us create an interface for CameraService.java and service for CameraServiceImpl class, inside com.cameraservice.camera.services package.

CameraService.java

CameraServiceImpl.java

4. Developing Entity Class

Finally we create our entity class for our camera and name it as Camera.java inside com.cameraservice.camera.entity.

So we are done with developing our Camera Rest application. The next step is to start our camera application by right clicking on our application and then Run it as Spring Boot App.

We can open our postman client, a broadly used client for mimicking our front end server, it will help us in making rest api call.

Let’s start with very first api which is used to check if the application is working correctly and ofcourse return the world famous ' Hello World’.

1. Api to check if the application is working correctly.
API route: http://localhost:8080
Method: GET.

2. Api to get the list of camera list.
API route: http://localhost:8080/camera
Method: GET

3. API to get the camera filter based upon on the camera unique id.
API route:
http://localhost:8080/camera/1
Method: GET

4. API help in adding new camera into the list.
API Route
localhost:8080/camera
Method: POST.

These is all for the the Restful service that we built using Java spring boot framework, as promised we will be exposing 4 rest API i.e.

  • To get the list of camera.
  • To add a new camera.
  • To get a specific camera based upon camera id.
  • The initial testing api, return “Hello world”.

Read, update and delete will be a task, just to implement business logic for these functionality and you will be good to understand about the concepts.

In the subsequent tutorial we will be exploring about how can we connect our application with the database. Since we are using an in-memory storage in the current tutorial, so risk is to lose the data once we rebuild our application.
But with the help of database we can store the data for as long as we want.

Github Code Link: https://github.com/apoorvtomar2222/camera-management-system/

Branch: Master

About the Author

Apoorv Tomar is a developer by profession and the man behind Mindroast. You can connect with him on Twitter and also find some good technical content on Youtube.

--

--