Software Engineering

Spring Boot For Beginner Java Developer

Spring Boot is a framework that has many features for Java development. It offers fast and production-ready application. If you already learn about Java fundamental such as data type, method, class, etc. Then you already have experience to create a simple application like calculator run on terminal, now is your turn to feel the powerful of Spring Boot.

As describe on Spring Boot website, it’s a stand-alone application that you can “just run”. Because it has an ability called Auto Configure, for example in fundamental course we want to build an application that connect to database, we should create a singleton configuration code to enable your application to do a database query. In Spring Boot we only need to do 2 simple things:

  1. Add the database client dependency
  2. add database configuration such as host, database name, username, and password

For starting point, we will create a To Do Rest API. It contains an endpoint to store a data and endpoint to get it.

Start with Installations

Below is the requirements before we can dive into Spring Boot

  1. Java Installed. Download here.
  2. Intellij IDE. Alternatives: Netbeans, Spring Tools Suite
  3. Internet of course

Create a “To Do” project

For this one, we have two options based on IDE we use.

  1. Spring Tools Suite, it has a feature to create a spring boot project. So, we can use it as simple as click a button on toolbar.
  2. Others, create project using Spring Initializr.
  3. Or buy the Intellij Ultimate Edition.

In this article, because of I am using Intellij Community Edition I choose the second one. Go to the website , fill the forms then click Generate.

spring initializr default page
Figure 1 – Spring Initializr Generator Page

Describes below:

Projectwhich dependency management do we choose for the application
LangugeSpring Boot support these language. Choose one that you familiar or expertise
VersionDefault is the recommendation
Meta DataFill it whatever you want
Package NameOur default package name for your application
Packagingif our web server is Apache Tomcat choose WAR. Or if you do a stand alone application choose JAR
Java VersionShould have the same version between your local and the server
DependenciesAdd as many as you need

When you click Generate Button, you will download the templates with your preference in the page. Then, open the project with your IDE.

Look at POM File

I am using Maven so I will have a pom.xml as a meta data of our application along with dependency management.

pom file
Figure 2 – pom.xml

POM File construction is describes below:

mark 1details of our application
mark 2variables that will be use in other sections
mark 3list of dependencies
mark 4list of plugins and compiling configuration

So whenever you need a dependency, simply adding to dependencies tag.

Folder Structure

When we working on software engineering, folder structure is the thing that we have to understand.

spring boot folders structure
figure 4 – Spring Boot folder structure

I think to create a good application, we have to understand the function of these folder structure. I already mark the important folder / file that will we need on build Spring Boot application.

Mark 1default package where we working on this application. Put all Java classes to that package, even create new folder / sub package
Mark 2if we create a Spring MVC application that contains a web page we will use these folder. Static will contains a assets like css file, js file, default images, etc
Mark 3contains a configuration that will be use on application. Build in properties file or yml file
Mark 4contains a Unit Test files
Mark 5Maven file as meta data of our application

Application Class

Before going to Controller, let’s look at application file for a moment.

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

For a Java Developer, we already understand of psvm method. But as we see, there’s an annotation called @SpringBootApplication. The annotation will tell the application that this application is a Spring Boot Application. So this application could use all features of Spring Boot. The powerful one is the annotation itself.

Spring Boot has so many complex feature, but easy to use for developer. As simple as give an annotation to a variables or methods, even classes. We will discuss about it later.

Create a Controller

Now is the time, we will make an REST API for user to use our features. It’s better to make a new package first to specified the Controller Classes. Then create a controller file inside the package.

@RestController
@RequestMapping(value = "/todo", produces = MediaType.APPLICATION_JSON_VALUE)
public class TodoController {

    @GetMapping
    public String findTodo() {
        return "Will contains a list of todos";
    }
}

We are create an GET endpoint that point to /todo. Then we are expecting the result is a text “Will contains a list of todos”. Let’s run the application,

toolbar to run or debug the application
Figure 5 – Run Application

You will see in terminal

terminal information after run the application
Figure 6 – Terminal info

It says, application running on port 8080.. So let’s open our browser and type localhost:8080 then Enter

a result of our controller
Figure 7 – The result

Tadaa… We are success to create our first Spring Boot Rest API. I think it’s enough for this session. I encourage you to try it by yourself. Next things will be more excited, we try to save it to database and get all the todos. Bye…

Leave a Reply

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