Springboot from the beginning

Lahiru Sampath Abegunawardena
4 min readOct 27, 2019

Hi I’m lahiru Abegunawardena and this a new series of articles I start on Springboot. I hope you will find this articles helpful. So let’s begin.

What is Springboot?

As you already know when we develop a web application we can use a RESTful web services for managing our database and do required CRUD operation. This type of web services can use sucessfully with frameworks such as Angular and libraries such as React. All you need to do is run them with different ports in a server and call to service using api end-points and perform correct CRUD operation.

So Spring boot is one of most popular RESTful web service used around the world. So in this tutorial I use Springboot for creating my service and use Mysql as my database server.

Build your project..

Let’s start from the beginning. Springboot is based on Java. So you need Java to start this. If you don’t have Java installed in your Computer please install and continue this. I’m using Java 1.8 in my computer with IntelliJ as my IDE.

There are few ways to start developing a Spring project but I will discuss with you about how to start this as a Maven Project.

Go to File->New->Project. Then you will get following dialog box.

Don’t tick “create from archtype” and press Next..

You can add details for group Id and ArtifactId then click Next..

Then Click on finish. You will now looking at the generated pom.xml file in your Maven Project. Now add following block.

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

Now your IDE will ask to import required so allow them and it’ll install all dependencies to run your project. This will take some time.

Let’s Start Coding..

So Now we can start our project and run it. You need to create a new package with name as you give in group id. So I create it as “com.springboot.blog” and Java Class as “ApplicationMain” in that newly created package. And you have to tell java that this is main class that executes a SpringBoot application, so we use Annotation in order to achieve this, annotation we need here is “@SpringBootApplication”. So as you can believe we also need Main method in this class, because this is the main class that executes whole RESTful Service in our computer.

Now we need to start our Springboot application from this class, we need a small function in order to continue this as “SpringApplication.run(ApplicationMain.class, args);”.

So if you follw all this steps correctly you will find that class like this at the end.

So let’s start our service. You can right click on your file and click on “Run ApplicationMain” to execute your project easily on IntelliJ. And you will find your IDE shows a result as below.

You will find out that Spring application runs in apache port 8080. But if you open your browser and set url to “ http://localhost:8080/” you will find out page like this.

This is because we didn’t set any listeners to this api-end. What we should do. We need to write a Controller to accept this url and return something meaningful.

Let’s create new package named ‘com.springboot.blog/Controllers’ and Create another class named “HomeController” in that package.

We can indicate this as a controller class to spring by using annotation “@RestController”.

Let’s make this contoller to listen this url and return meaningful straing.

Now I’ll explain you what this code does.

GetMapping(“/”)

You can listen to getrequest comes from url “http://localhost:8080/” and this function Home() returns string as “Hello World”.

If you saved the code and restart project from pressing Ctrl+F5. Then you can below response in your browser.

You can also get this response by sending get request through Postman as below.

So as you saw you can easily start a Spring project and start your RESTful service. From next week onwards we will discuss about how to perform CRUD operations with this beatiful framework.

Hope you will find this interesting. If there is something feel free to comment. Like and Share with your friends. Here are my linkedin and fbpage. So you can contact me easily.

--

--