Spring Boot - Quick Start



This chapter will teach you how to create a Spring Boot application using Maven and Gradle.

Prerequisites

Your system need to have the following minimum requirements to create a Spring Boot application −

  • Java 17
  • Maven 3.6.3
  • Gradle 7.5

Spring Boot CLI

The Spring Boot CLI is a command line tool and it allows us to run the Groovy scripts. This is the easiest way to create a Spring Boot application by using the Spring Boot Command Line Interface. You can create, run and test the application in command prompt itself.

This section explains you the steps involved in manual installation of Spring Boot CLI. For further help, you can use the following link: https://docs.spring.io/spring-boot/installing.html

You can also download the Spring CLI distribution from the Spring Software repository at: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-cli/3.3.3/spring-boot-cli-3.3.3-bin.zip

For manual installation, you need to use one of the following two archives −

  • spring-boot-cli-3.3.3-bin.zip

  • spring-boot-cli-3.3.3-bin.tar.gz

After the download, unpack the archive file and follow the steps given in the install.txt file. Note that it does not require any environment setup.

In Windows, go to the Spring Boot CLI bin directory in the command prompt and run the command spring -version to make sure spring CLI is installed correctly. After executing the command, you can see the spring CLI version as shown below −

E:\spring-3.3.3\bin&t; spring --version
Spring CLI v3.3.3
E:\spring-3.3.3\bin>

Create Welcome Message Web Application with Spring Boot CLI

E:\spring-3.3.3\bin> spring init --build maven -a test
Using service at https://start.spring.io
Content saved to 'test.zip'
E:\spring-3.3.3\bin>

Now you can check that a maven based spring boot project is created with following pom.xml

<?xml version="1.0" encoding="UTF-8"?>
   <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>3.3.3</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example</groupId>
   <artifactId>test</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>demo</name>
   <description>Demo project for Spring Boot</description>
   <url/>
   <licenses>
      <license/>
   </licenses>
   <developers>
      <developer/>
   </developers>
   <scm>
      <connection/>
      <developerConnection/>
      <tag/>
      <url/>
   </scm>
   <properties>
      <java.version>17</java.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

And a java class which acts as a main application class.

package com.example.test;

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

@SpringBootApplication
public class DemoApplication {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
   @RequestMapping("/")
	String home() {
		return "Hello World!";
	}
}

Let's add spring boot starter web dependency in pom.xml

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

And DemoApplication.java with an end point to print "Welcome to Tutorialspoint!"

package com.example.test;

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

@RestController
@SpringBootApplication
public class DemoApplication {

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

   @GetMapping("/")
   String home() {
      return "Welcome to Tutorialspoint!";
   }
}

Now run the application by using the maven command mvn spring-boot:run as shown below −

E:\spring-3.3.3\bin\test>mvn spring-boot:run

Once you run the command, required dependencies will download automatically and it will start the application in Tomcat 8080 port as shown in the screenshot given below −

[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:test >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> spring-boot:3.3.3:run (default-cli) > test-compile @ test >>>
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ test ---
[INFO] Copying 1 resource from src\main\resources to target\classes
[INFO] Copying 0 resource from src\main\resources to target\classes
[INFO]
[INFO] --- compiler:3.13.0:compile (default-compile) @ test ---
[INFO] Recompiling the module because of added or removed source files.
[INFO] Compiling 1 source file with javac [debug parameters release 17] to target\classes
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ test ---
[INFO] skip non existing resourceDirectory E:\spring-3.3.3\bin\test\src\test\resources
[INFO]
[INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ test ---
[INFO] Recompiling the module because of changed dependency.
[INFO] Compiling 1 source file with javac [debug parameters release 17] to target\test-classes
[INFO]
[INFO] <<< spring-boot:3.3.3:run (default-cli) < test-compile @ test <<<
[INFO]
[INFO]
[INFO] --- spring-boot:3.3.3:run (default-cli) @ test ---
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-buildpack-platform/3.3.3/spring-boot-buildpack-platform-3.3.3.pom
...
[INFO] Attaching agents: []

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.3.3)

2024-09-02T16:30:13.931+05:30  INFO 19320 --- [demo] [           main] com.example.test.DemoApplication         : Starting DemoApplication using Java 21.0.2 with PID 19320 (E:\spring-3.3.3\bin\test\target\classes started by Tutorialspoint in E:\spring-3.3.3\bin\test)
2024-09-02T16:30:13.937+05:30  INFO 19320 --- [demo] [           main] com.example.test.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2024-09-02T16:30:15.753+05:30  INFO 19320 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2024-09-02T16:30:15.801+05:30  INFO 19320 --- [demo] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-09-02T16:30:15.801+05:30  INFO 19320 --- [demo] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.28]
2024-09-02T16:30:15.975+05:30  INFO 19320 --- [demo] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-09-02T16:30:15.975+05:30  INFO 19320 --- [demo] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1946 ms
2024-09-02T16:30:16.625+05:30  INFO 19320 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2024-09-02T16:30:16.636+05:30  INFO 19320 --- [demo] [           main] com.example.test.DemoApplication         : Started DemoApplication in 3.707 seconds (process running for 4.316)

Once Tomcat starts, go to the web browser and hit the URL http://localhost:8080/ and you can see the output as shown.

Hello Spring Boot
Advertisements