Creating a Spring Boot application with Spring Initializr

In the first tutorial Hello World con Spring Boot we have learned how to create our first Spring Boot application from the Command Line. In this tutorial we will learn how to create quickly a Spring Boot application using the Cloud app known as Spring Boot initializr.

The Spring Initializr is availableat this address: http://start.spring.io

spring boot hello world initializr

You need to enter the required parameters for your build type (Maven o Gradle), and the version of Spring Boot you want to use. On the left side, you can specify the project metadata.  By clicking on “Switch to the full version” you can add extra parameters like the package which will be eventually added to your project build.
On the right side, you can add the dependency required for the project. If you have switched to the “full version” you will be able to see the list of all available dependencies. As we need REST Services, this will be our selection:

spring boot hello world initializr

Click on Generate Project to download the Project.

Loading the Spring Boot project in your IDE

Now you can load the project in your IDE. We will be using the Community Edition of IntelliJ which is available for Download at https://www.jetbrains.com/idea/ .Once downloaded, just unpack it and you are ready to work with it. Now choose to “Import Project” to load the Spring Boot project in it:

spring boot hello world initializr

Point to the folder where you have saved the Spring Boot project: (“simplerest” in our example):

spring boot hello world initializr

Choose to use Maven as Model:

spring boot hello world initializr

Here is your Spring Boot project in “Project Files” :

spring boot hello world initializr

As you can see, there’s already a Main class in the project:

package com.example.simplerest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Note the @SpringBootApplication annotation that allows component-scanning is the auto-configuration of your Spring Boot application. Indeed, this annotation combines three distinct features:

     @Configuration: Designates a class suitable for configuring the application using the Spring model.
     @ComponentScan: Enable component scanning so that the Web controller and other components will be automatically discovered and registered as Bean Spring in the application context.
     @EnableAutoConfiguration: This annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that were added. For example, if HSQLDB is on the classpath and no database connection beans have been configured manually, Spring automatically configures an H2 database in memory.

Having the main class, it’s time to add a Controller in order to have a REST service:

package com.example.simplerest;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {
  private static final String template = "Hello, %s!";

  @RequestMapping("/greeting")
  public String greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
    return String.format(template, name);
  }
}

@RestController means that the Response will returned directly without being elaborated by a template. The application is ready and we can run it as follows:

$ mvn clean spring-boot:run

As you can see from the console, the application is starting up:

spring boot hello world initializr

In order to test it you can reach for the following URL: http://localhost:8080/greeting?name=(name) :

spring boot hello world initializr

Great! we just created our first Spring Boot application in a matter of minutes and imported it into IntelliJ!

Found the article helpful? if so please follow us on Socials
Twitter Icon       Facebook Icon       LinkedIn Icon       Mastodon Icon