Spring Boot Tutorial

Spring Boot Tutorial

March 22, 2017

This is a quickstart tutorial for Spring Boot. We will be building out a simple Spring Boot REST example from scratch using Maven.

Spring Boot Tutorial: Getting Started

This tutorial assumes you have Maven installed. If not, please install the latest stable version of Maven. To get started, create a new directory and cd into it. Run the following to create the basic Spring Boot app structure:

mkdir -p src/main/java/webapp

This creates the basic file structure for our Spring Boot project. Now lets add our pom.xml file to the root directory of the project. This file contains the configuration details behind your Maven build, including any required dependencies and plugins for the project. Once created, copy the following into the pom.xml file:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework</groupId>
    <artifactId>hello-spring-boot</artifactId>
    <version>0.1.0</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

This adds the required dependencies for Spring Boot as well as the Spring Boot Maven plugin.

Creating The REST Controller

To create a sample REST endpoint, we add a Java class with the Spring Boot REST controller annotations. Under the webapp directory, add a WebAppController.java.

WebAppController.java

package webapp;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class WebAppController {

    @RequestMapping("/")
    public String mainPage() {
        return "Hello World";
   }

}

This creates a basic REST endpoint defined by the @RequestMapping("/"). Notice how we define our route /. in the @RequestMapping annotation. We then simply return a string, "Hello World", to the browser.

Creating Spring Boot App

Under the webapp directory, create another java class Application.java.

Application.java

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

@SpringBootApplication
public class Application {

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

}

This creates the launch script for our Spring Boot instance.

Running the App

Navigate to the root of the project and run the following maven command:

mvn clean package

This will build the Spring Boot project using Maven. Once complete, run:

java -jar target/hello-spring-boot-0.1.0.jar

This runs the executable jar file produced by the mvn clean package and stored under the /target directory.

You should see the local port printed in the console. Navigate to the localhost, such as localhost:8080, in the browser. If all went according to plan, you should see the worlds "Hello World" on the screen.

Conclusion

That's it. You've created a basic Spring Boot REST example and run a local instance of the app. You'll probably want to return more than just strings to the browser so remember that there are plenty of template engines for Spring (Thymeleaf) that make rendering client rich web apps a breeze.


Join the conversation...

Posted by Roopesh Kumariv
May 11, 2017

spring boot is super easy to set up. it definitely gives node.js/express a run for it's money. while traditional spring had a ton of boilerplate code none of us like, this simple set up makes SpringBoot better than node.js in my opinion, mainly because of all the benefits you get with using something that is Java based vs JavaScript based.
Posted by Bindui S.
April 7, 2017

um more please. this made spring boot an absolute breeze. thanks for just giving me the info and skipping (most of) the bs. can you please produce more stuff like this. i know tons of people looking to get into java and spring and this was a great tutorial.
Posted by Sam E.
March 22, 2017

the hard part is installing maven if you ask me :)
Posted by Terry Neumont
March 22, 2017

super fast tutorial. it works!


More by the stackchief