Your first Spring Boot Web Application - Hello World

May 26, 2021 No comments Spring Boot Application Hello World

1. What is Spring Boot

Spring Boot is a platform crafted to build mainly Spring-based applications. It was designed to create production-ready systems fast with minimum configuration and many things supported behind the scenes.

Some of the best Spring Boot features are:

  • it can be run like any other java stand-alone application - no need to deploy WAR files,
  • reduce development time - convention-over-configuration approach,
  • can automatically configure Spring and 3rd party libraries,
  • no more XML configuration hell, everything can be configured with annotations.

2. Requirements

Spring Boot 2 requires from Java 8 to Java 11 to run. We recommend installing OpenJDK if you don't have it already on your OS. Additionally to build and deploy Spring Boot applications we will also need:

3. Creating Your First Spring Boot Application

If we are sure that Java and Maven are installed on our operation system we can start developing our first Spring Boot application that will print 'Hello World!'.

To start, first we need to create a project using our favorite IDE or just creating a simple empty folder, and put pom.xml file with the following content:

<?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>

    <groupId>com.frontbackend</groupId>
    <artifactId>hello-world</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>

</project>

We will use maven to build our app so special folder structure is needed:

  • src/main/java - source directory,
  • src/main/resources - resources directory (we will put here configuration files like for example application.properties).

Spring Boot comes with many special libraries called 'Starters', that can be used to extend your application with new features. We alredy used base starter spring-boot-starter-parent - it contains useful Maven defaults and provides dependency-management section. Since we are building a web application, we will also need to add spring-boot-starter-web dependency to our pom.xml:

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

Run mvn dependency:tree command to see if our Spring Boot application is a fully MVC project with dependencies like Tomcat web server, Spring core library, logback and Spring Boot itself.

To finish our 'Hello World!' application we need to create a single Java class with public static void main method that fires Spring Boot application.

package com.frontbackend;

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

@RestController
@EnableAutoConfiguration
public class Application {

    @RequestMapping("/")
    String index() {
        return "Hello World!";
    }

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

Application class was described with the following annotations:

  • @RestController - is a stereotype annotation treat as a hint that this class will be accepting HTTP requests,
  • @EnableAutoConfiguration - that magic annotation makes Spring Boot automatically configure the application based on the dependencies in pom.xml.

@RequestMapping annotation set on index() method gives "routing" information to Spring Boot that HTTP requests with the / path will be from now handled with index() method.

4. Running Spring Boot Web Application

There are 3 methods to run Spring Boot application:

a) run from IDE

Use IDE to run Spring Boot application. For instance in InteliJ you just need to click green arrow button located near the Application class.

Run spring boot from intelij

b) java -jar

To create executable jar file for Spring Boot application first we need to add a special maven plugin spring-boot-maven-plugin in our pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

First build our project with mvn clean install command, and after SUCCESS result, go to target folder. There should be hello-world-0.0.1-SNAPSHOT.jar file there.

We can run Spring Boot from here using below command: java -jar hello-world-0.0.1-SNAPSHOT.jar

b) mvn spring-boot:run

In project root folder type mvn spring-boot:run command that should build your project and start Spring Boot right after that.

No matter what method we choose, if everything is correct and port 8080 is not occupied, there should be an output simillary to this one:

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

2019-05-24 23:52:55.431  INFO 5326 --- [           main] com.frontbackend.Application             : Starting Application on dell with PID 5326 (/home/frontbackend/projects/tutorials/spring-boot/hello-world/target/classes started by frontbackend in /home/frontbackend/projects/tutorials/spring-boot/hello-world)
2019-05-24 23:52:55.435  INFO 5326 --- [           main] com.frontbackend.Application             : No active profile set, falling back to default profiles: default
2019-05-24 23:52:56.558  INFO 5326 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-05-24 23:52:56.593  INFO 5326 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-05-24 23:52:56.593  INFO 5326 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.19]
2019-05-24 23:52:56.685  INFO 5326 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-05-24 23:52:56.685  INFO 5326 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1208 ms
2019-05-24 23:52:56.892  INFO 5326 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-05-24 23:52:57.122  INFO 5326 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-05-24 23:52:57.126  INFO 5326 --- [           main] com.frontbackend.Application             : Started Application in 2.082 seconds (JVM running for 4.669)

The last line tell us that server is up and running: Started Application in 2.082 seconds (JVM running for 4.669)

Now we can go to our browser address bar and enter http://locahost:8080 (8080 is a default Spring Boot port, if you want to change that port go here -> How to change Spring Boot port). There should be 'Hello World!' text.

Spring boot hello world browser

You can download code used in this tutorial from GitHub:

https://github.com/martinwojtus/tutorials/tree/master/spring-boot/hello-world

{{ message }}

{{ 'Comments are closed.' | trans }}