How to Make a Simple API for Your IoT Project in 10 Minutes — Getting Started with REST and Spring Boot

Rashmin Mudunkotuwa
7 min readAug 13, 2019

Actually What is an API?

If you clicked on this link you must be wondering ‘what in the world is an API ?’ or ‘How can I use it effectively in my project ?’. As this is a simple introduction to the world of APIs, lets first define what an API is and how can it be possibly used in your project.

Simply put, API is a set of code which is used for communication between two software programs. You can find a better/more technical definition here. Let's understand this by relating it to the IoT domain use case. Imagine you have an Arduino Uno or an ESP8266 equipped with a set of weather sensors and you want to have a weather database remotely to record all the data so that they can be used later for analyzing purposes. For this, obviously, you first need a database, but even if you made a database, how to communicate with it? This is where APIs come in handy. You can place an API in the middle of the Arduino App and the Database and use it to handle the communication between them.

Here in this article, I’m talking about a special kind of API which is a REST API. The special thing about a REST API is that it uses HTTP protocol methods such as GET and POST to handle its requests. So basically it implements a request and response strategy. Now don’t worry if you don’t know what GET and POST are. Simply think of them as ways to request something from an API.

If I take an example, imagine you implemented a REST API, then it has some methods which can be used by outside parties through an URL. If you have a method named savedata() in your API which accepts some data from a remote source and stores them in a database, you must be wondering how to access that method and how to send a particular set of data. It is a simple process! Methods in a REST API can be assigned with a URL of our choosing so that we can access that method remotely. For an example that link could be, www.yourapi.com/data/savedata, if you send a request to this method you can access that method in the API. Then how to send data along with the URL, here where those GET/POST words come in to play.

In the GET method, data is sent as URL parameters as name value pairs and they are visible in the URL. In our example, we can send a sensor reading we obtained to the save data method easily using GET methodology, the URL will look like this

www.yourapi.com/data/savedata?name=temperature&value=32

So if we sent a request to this method like this, the savedata method will grab the parameters name and value and we can write any logic in the API process that data and store them!

In a POST method data is sent in the body of the HTTP request, we will talk about POST requests in a later article.

I think now you have a basic understanding of how an API works and how it is used. Now let’s make a simple REST API which will return a string ‘Hello World’ when you send a request to it.

To make a REST API, there are solutions available in many languages and frameworks. I’m going to use the Spring Boot Framework which is based on Java to make the API.

For this you will have to install Java 8.0 (Programming Language), Maven(Build Tool) and IntelliJ IDEA (IDE) in your computer.

After installing these, open IntelliJ IDEA and select Create a New project and from that menu select Maven Project,

Then give a group ID and a artifact ID for you project. These are used to identify a project separately from other projects in the Maven Build tool. You can learn about Maven more from here.

To create a basic Spring Boot project you have to provide a set of dependencies in the POM.xml file which is created when a new Maven Project is created. Dependancy can be explained as an online resource/library which we are going to import in our project and use. Modify your pom.xml file as below.

Then you have to make two new packages and two classes inside the java folder in the project structure. Below is the project structure. The controller package and the Application class is inside the testApp package. Controller class is inside the controller package.

Here Controller Class can be named as the class where we define the methods which are used to handle the requests sent by the user. Application class is the class which is used to bind all the classes which are used in a Spring Boot Application (Bootstrapping) and create a single application.

Let's look at the Controller class first. One thing you want to know before designing the class is that in Spring Boot we use Annotations to indicate that a certain class or a method is doing some specialized work. You will get the hold of annotations when you design a program by yourself. You can learn more about java annotations here.

In the Controller class, we use @RestController, annotation to indicate that the following class is working as a controller which handles users requests. and @RequestMapping(“/”) annotation to indicate that all URLs ending with (“/”) are forwarded here.

@RestController
@RequestMapping("/")
public class Controller {..}

Then we create a simple method to return a String “Hello World” which we want to be triggered when a user sends a request.

public String hello(){return "Hello World"}

But how we indicate the URL which the method will be associated with. For that we can use @GetMapping Annotation, it specifies that a certain method is a GET method. In the parameters section of the annotation, we can give the required URL.

@GetMapping("/hello")
public String hello(){
return "Hello World"}

Now the Controller class is finished. As the final step let’s design the Application class. We use @SpringBootApplication annotation to implies that the class is the class that will bootstrap everything.

@SpringBootApplication
public class Application {..}

Inside the class create a main method and put the below code.

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}

Here, inside the SpringApplication.run() method, we have to give the name of our Application class and args as parameters.

Congratulations! Now you have made you first Spring Boot Application. To launch the server, just run the main method.

If the build is successful you will get the following result in the Run window at the bottom of the screen.

Here says that the server was launched in port 8080 in your localhost environment. So, to access the server you can use http://localhost:8080 address. Then, to access the method you wrote, you could use, http://localhost:8080/hello URL.

After launching the server paste that link in your web browser and you can see the response you got from the server!

That step concludes this short introduction to the world of APIs. Remember that the contents here are only a brief introduction to get a basic idea. I have shared the full project in GitHub. I hope to give a more thorough explanation in the upcoming articles. Feel free to ask any question you have. You can contact me through LinkedIn or Medium :)

--

--

Rashmin Mudunkotuwa

Software Engineer | Interested in Cloud Computing, Microservices, API Development, and Software as a whole.