Booting Spring Boot with IntelliJ IDEA

Booting Spring Boot with IntelliJ IDEA

Java is one of the most popular programming languages running on more than 3 billion devices! In the Enterprise side of Java, Spring Framework has been in the industry for a long time. Spring Boot is Spring’s convention over configuration solution to create a standalone, production-grade Spring application that just runs. Some of the core features that make Spring Boot standout to build production-ready applications are

  • No XML based configuration instead annotations are used

  • Embedded Tomcat, Jetty, and Undertow, no need to deploy WAR

  • Opinionated starter dependencies to simplify build configuration

  • Support for production-grade features like metrics and health checks

Spring makes Java simple, modern, productive, reactive, and cloud-ready. In April 2014, 1st version of Spring Boot 1.0.0 was released

Here we will discuss how to start your Spring Boot application using one of the most popular IDEs available for Enterprise Java programming, JetBrains’ IntelliJ IDEA.

Prerequisites

JetBrains IntelliJ IDEA has multiple flavors of IDE, with Community Edition (CE) which is free of cost, Ultimate Edition for web and enterprise app development, and an EDU Edition to support learners and educators. For the purpose of this beginner tutorial, it is sufficient to download the CE Edition. For comparison of different Editions visithere.

Let us use Spring Initializr to create a simple Gradle project.

Gradleis a build tool that can be used to automate building, testing, deployment, etc

Select the below configurations and GENERATE the project. You will have a zip file generated and save it to your local file system. Unzip the file and save the folder.

  • Project — Gradle project (Another popular build tool is Maven)

  • Language — Java (we can create Spring Boot application for other programming languages like Kotlin, Groovy)

  • Spring Boot — Latest version

  • Project Metadata — Choose group, artifact, name, description, package name (leave the default values if you want to)

  • Packaging — JAR (We can also deploy Spring Boot application as WAR)

  • Java — Latest version

Fire up your Intellij IDEA IDE. The startup screen should look something as shown below (I am using Ubuntu 20.04)

IDE stands for Integrated Development Environment which comes with a suite of tools for maintaining your source code, building, compilation, code autocompletion and so many powerful features that you can’t get in a simple text editor.

Click Open and navigate to the folder that you have unzipped and select the folder. You will get a warning message, you can click Trust Project

You can see the project window opens.

As a beginner don’t get overwhelmed by the generated code snippet. Navigate to src > main > java and to the package and the class that has been created with the above project metadata that you have generated using the Spring Boot Initializr. It will look something like below.

I have been using a dark theme*Dracula*and it is your personal choice to use a light or dark theme in your IDE

Now we have opened the default code generated from the Spring Boot Initializr, let us little bit understand the code before we start with our booting Spring Boot application.

The above code snippet that is generated is the starting point of the Spring Boot program. @SpringBootApplication annotation includes @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan. All Java program starts with the main method and here we run the Spring Application.

We are going to create a simple REST endpoint. For that go to build.gradle file and add the below dependency.

We need to make sure to Add Configuration to build our project. On the top right of the IDE you will find Add Configuration button, click on it and you will get the below popup.

Add a new Gradle configuration and click Apply and Ok.

Now we can go ahead and build our project. Click the wrench icon as shown below to build the project.

Building the project will take some time if you are doing it for the first time as it has to pull all the dependencies for the project.

Hurray! if you have followed all the above steps and if there are no errors, your project would have been successfully built and you will see the output as shown below.

Now let us have some fun in writing code to print hello world by creating a REST endpoint. Go to src > java > main and to the package and the class that has been already created. Add @RestController annotation to the top of the class and @GetMapping annotation and create a method as shown below. All we are doing here is to add a simple REST endpoint to the Spring Boot web application.

Now you have written your simple REST endpoint let us build the project and run it. Once the build is successful with no errors, click on the Run button as shown below.

The application runs in the embedded Tomcat container, and you should be able to go to the browser to the URL http://localhost:8080 and see the below message printed on the screen.

By default the embedded Tomcat server will run in port but it can be configured to run in another port

Let us add a query parameter to the REST endpoint that we have created. Your code will look as below.

Here we have added a new request param name and you can pass the value as a query string and if you don't the default value is world. Run the application and you can go to the browser to the URL http://localhost:8080/hello and http://localhost:8080/hello?name=elonmusk and see the results.

A big congratulations! 🥳 You have successfully written your first hello world Spring Boot application and also understood how to pass a query parameter to the REST endpoint. Both Spring Boot and IntelliJ IDEA are powerful tools for building Enterprise class Java applications. Play with it and let us start building some Enterprise-grade Java applications.

Thank you for reading! If you have enjoyed it, please comment on it!


I am a seasoned engineering leader with extensive experience building enterprise cloud and mobile platforms while knitting together high-performing engineering teams. I inspire engineers to get the trains to run on time. I’m passionate about travel, collaboration, and shaping new engineers through my activities as a blogger, speaker, and course author. I have been sharing my thoughts on life, engineering, and productivity 🚀

Website|Blog|LinkedIn