How to Create Spring Boot Project using Spring Initializr?

Spring boot provides a starter project called Spring Initializr which provides you a template for your project based on the options you choose on the starter project.

Overview

Spring boot provides a platform for rapid application development that are production level and can directly be run without needing an external application server. Spring boot has embedded servers with it which just need to be enabled by adding a dependency. Hence there is no need to deploy an external server.

In this article we will go through the steps to develop and run a basic hello world application. For this purpose, we will use the following technologies:

  • Java 8
  • Spring boot 2.7.8
  • Maven 3.6
  • IntelliJ idea

There are two ways to create a spring boot project.

  • Spring Initializr: This is a template project hosted on https://start.spring.io. In this project you configure the project details and download the template project and import it into your IDE.
  • Manual Project Setup: This is a traditional project setup where you create a maven/Gradle project and add dependencies yourself.

For the scope of this article, we will only cover the Initializr project which is also the easier one.

Configure Spring Initializr

Spring boot provides a starter project called Spring Initializr which provides you a template for your project based on the options you choose on the starter project. This initializr is also available in IntelliJ in a wizard form.

These options are the following:

  • Project build tool
  • Language
  • Spring boot version
  • Project metadata (group, artifact name, package name etc.)
  • Packaging type (jar or war)
  • Language version
  • Dependencies

See the below screen for detailed view.

Once you fill all the required details and download the project, simply import it into your IDE (IntelliJ in our case).

As you can see, we have added a dependency called spring web which is a minimum for building web application and rest Api. This uses tomcat as default container.

Run the application

Now that you have imported the project, go to the main class and run it. Your application should run in no time, and you should see the following logs.

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.2)

2022-12-31 23:51:13.163  INFO 13480 --- [           main] c.e.helloworld.HelloWorldApplication     : Starting HelloWorldApplication using Java 1.8.0_265 on DESKTOP-CQOMV5C with PID 13480 (D:\project-inter\hello-world\target\classes started by xyz in D:\project-inter)
2022-12-31 23:51:13.163  INFO 13480 --- [           main] c.e.helloworld.HelloWorldApplication     : No active profile set, falling back to default profiles: default
2022-12-31 23:51:13.937  INFO 13480 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-12-31 23:51:13.953  INFO 13480 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-12-31 23:51:13.953  INFO 13480 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-12-31 23:51:14.030  INFO 13480 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-12-31 23:51:14.030  INFO 13480 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 820 ms
2022-12-31 23:51:14.332  INFO 13480 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-12-31 23:51:14.348  INFO 13480 --- [           main] c.e.helloworld.HelloWorldApplication     : Started HelloWorldApplication in 1.505 seconds (JVM running for 2.101)
2022-12-31 23:51:22.814  INFO 13480 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-12-31 23:51:22.816  INFO 13480 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-12-31 23:51:22.818  INFO 13480 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

If you see these logs, it means you have successfully built the application. Next step is to write a REST Api which is the inherent purpose of this post.

Add a REST controller

Now we need to add a rest controller and an endpoint.

  1. For the controller, you can write a separate class and annotate it with @RestController or use the main class (which is already there) as a rest controller by simply annotating it with @RestController. For the sake of simplicity, we will use the main class.
  2. Add a method in the controller which returns string and annotate it with @RequestMapping("/hello"). This is the endpoint, and this method will be executed when this URL is hit.

Now your main class should look like this.

package com.example.helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class HelloWorldApplication {

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

  @RequestMapping("/hello")
  public String hello() {
    return "hello world";
  }
}

Run the application and hit this URL and you should see ‘hello world’.