WS2811 POV display on PC fan

How to create a Spring Boot web application using Intellij IDEA Community Edition

На русском

Hello! This blog post focuses on one simple task: quickly creating a web application using Spring Boot in IntelliJ IDEA Community Edition.

For those having EAP or Ultimate version installed, you may refer to this tutorial that takes the built-in Spring Initializr project creator into account. The less fortunate folks (like me) will have to do slightly more work, but will also benefit from learning  the nitty-gritty of spring-based projects and maven. And, to be fair, that's not much more work than using the Initializr plugin in advanced IntelliJ versions.

There are two approaches to making a Spring Boot project. One of them is using Spring Initializr web application to generate your project template, which is supposedly easier, and another is manually adding the dependencies into your barebone Maven project.

Personally, I found that both approaches take about the same amount of time, so pick any one of them - it won't siphon more your precious time. To use the former approach, start from step 1, if you'd like the latter you may skip to step 4 immediately.

Let us begin, then!

Step 1. Create a maven project

  • It all starts with a project. Create a new project on Welcome screen:
 
  •  Pick Maven from type list. Click Next.
  •  Now you'll have to come up with GroupId and ArtifactId for your project. Refer to this page from Maven guide to learn about this. Generally, GroupId is your (or your company's) web domain in reverse. If you don't have one, just use your name or nickname, i.e. com.johndoe . ArtifactId is the name of your project.
  •  Pick your project's name and location. I usually name my projects just like ArtifactId to avoid confusion.
  •  That's it! You have your project now. Why's it have Maven type? Because it, first of all, it allows you to easily pull any kind of dependency into your project by just typing its GroupId and ArtifactId, and secondly it creates your project with well-defined folder structure that is considered de-facto standard for Java apps. You can learn more about it here.

Step 2. Add Spring boot dependencies

  •  Paste those to your pom.xml and enable dependency auto-import to avoid the fuss of continuously reimporting dependencies each time you add, remove or change one.

Step 3. Create a minimal set of classes

  • Now that we have our libraries, we're going to need to create a minimally viable code to make those libraries work. First of all, you want to follow the best practices and create a root package in src/main/java directory named after your project name.
Should be com.shortn0tes.springbootapp according to best practices.We all make mistakes sometimes...
  • Now, Spring Boot launches Tomcat servlet container and your application when you launch this method:
    public static void main(String[] args) {
    Let's create a class containing one:
  • Paste this code snippet into your new class: Don't forget the @SpringBootApplication annotation! It makes it all tick...
  • As you must have noticed, the dependencies have not been added yet:


    To import the dependency automatically, center your mouse on the red-colored class name and press Alt+Enter:

Step 4. Use Spring Initializr

Spring Initializr usage in pretty straightforward - you navigate to the website, set up your project details and get a Zip archive upon clicking the big green "Generate" button.

Unpack it someplace and that's what you're supposed to see:

Step 5. Import the generated project into IntelliJ IDEA

  • Instead of creating a project, click Import project now.
  • The first two steps are important: you're supposed to point to your unzipped folder and pick Maven as your import's model.

What comes next is essentially clicking Next until you get your new project imported to IntelliJ IDEA. Yeah, it's as simple as that.

Step 6. Create a launch configuration

  • Let's create the launch configuration to start our application. First, click Edit configurations...
  • Create a new configuration of Application type. Name it and pick your class containing main() method to launch:

  • And that's actually all. Click the Run button (alternatively, press Shift+F10). You'll see that your app has started on URL http://localhost:8080 in build output window:

To make sure it works, though, you might want to make it a bit interactive...

Step 7. (Optional) Add at least one controller

  • Controllers are a part of MVC pattern. Spring lets you easily create controllers with just a couple of annotations added to your class. Create a new package for your controllers under your root package, along with a new class for controller:
Spring boot will automatically recursively scan the root package (i.e. one containing class with public static void main() method and annotated with @SpringBootApplication) and register all the beans in application's context, including your newly-created controller. Now you can launch your web app again and see it working in your favorite browser:


As per usual, the complete project can be found HERE.

Addendum

Just for the sake of completeness, I'll show how to add any kind of dependency into your Maven project. You don't have to do it to create your Spring Boot app, but it helps to know where to start in your development.
  1. Search <your library name here> maven.
  2. People mostly pick either the latest stable version (5.0.0 in this case) or the most used one.
  3. Copy the text to your pom.xml, specifically the <dependencies> section.

Comments

  1. I followed the steps exactly (1-3, and 6) and it was easy to follow. I do have a problem though. When I click on Run 'Application' I get the following message:

    Cannot start compiler: the SDK is not specified for module "myModuleName".
    Specify the SDK in the Project Structure dialog.

    I was unsuccessful to select any Java directory for the SDK. What should I do?

    ReplyDelete
    Replies
    1. First, download JDK : www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html (or locate one installed on your pc, if you have one.). Then go to module settings of your project (hotkey F4) and specify the location of JDK here.
      Here's my configured project as an example:
      https://imgur.com/xuSZpZh

      The thing is, if IDEA hadn't managed to autodetect JDK, you probably don't have it installed. Usually if you install JDK first, then open Intellij IDEA and create a project, you don't have to do this.

      Delete

Post a Comment