Create your first Web App using Spring Boot with IntelliJ

Hasitha Kaushan
FAUN — Developer Community 🐾
5 min readMar 15, 2020

--

In this short vacation(unexpected) I decided to have a go and publish some articles related to spring boot. Let’s get started with having a brief understanding of Spring Boot.

In the early days, developers used Java EE(Enterprise Edition) to develop business applications. They had to use a lot of Java EE features by that time and had to manage them which was a difficult task. At the same time projects became heavy with the increment use of entities. Thus, a framework called Spring came up to the scene as a solution with a lot of features such as POJO, Dependency Injection, MVC, REST, Security etc. At the same time, Spring could be integrated with other frameworks such as Hibernate. But still, there is a major drawback of the Spring framework i.e. developers having to work with the configuration. It is a tough ask to handle the configurations while developing the business logic. If you are a developer, you know it is quite a mess to look at the configuration part while you work with the business logic 😫

Layers of deploying of a Spring App

To overcome these problems, a framework called Spring Boot comes in between the Developer and the Spring framework. One another great feature of Spring Boot is that it has an embedded server(tomcat) with it. Simply you have to build a jar file and you can deploy in any given JVM. Cool! Isn’t it? 🙌

Layers of deploying of a Spring Boot App

Now that is that about spring boot and let’s create the application. (a simple web application) 😍

Go to https://start.spring.io. There you will be able to bootstrap an app and generate it.

Spring Initializer

As I wanted to use this same application in one of my future posts regarding deploying of a spring boot app in a separate tomcat server, I have selected packaging as WAR.

As this is just a sample example, I have added Spring DevTools.

Now it’s done and generate your application. A ZIP file will be downloaded. Yes! you have generated your first Spring Boot application 👊 Now you have to import the generated file using your favorite IDE. Mine is IntelliJ ❤️

Next up, extract your downloaded ZIP file and import it. Select Import Project. Locate to the pom.xml file of the extracted folder. A window similar to the following will be displayed regarding the importing of the maven project and you will have to click next in order to keep the default settings.

Wohaaa!!! You have imported the spring boot project into IntelliJ 👊 If you have successfully imported the project, you will be able to see the following screen.

Cool ❤ Now let’s create a simple controller to test the application. Create a new package called controller.

Within the controller package let’s create a simple controller class called VersionController.java. In this simple example, I will show how to return a simple String for a particular request.

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@RestController
public class VersionController {

@GetMapping("/version")
public String getVersion(){
return "1.0";
}
}

The “@RestController” annotation indicates the class as a request handler for RESTful web services. It simply means that the particular class will handle web requests. The “@GetMapping” annotation indicates that the particular method will handle a GET request with “<host>:8080/version” URL (8080 is the default tomcat port).

Now it is all done and let’s test our application. Goto Run > Run ‘DemoApplication’ in order to run your application.

If it’s alright, you will see this dream message. 😃

Now let’s access it using the browser as we have created only a GetMapping we don’t need a tool like PostMan to access our resource. Locate to localhost:8080/version and you will see the following 💪

Yes! You’ve made it. ❤ See you soon in my next article. Until then, Happy Coding ❤️

👋 Join FAUN today and receive similar stories each week in your inbox! Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--

I am an Electrical Engineering undergraduate at University of Moratuwa. My passion is towards the Cloud Computing and IoT.