Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring Boot: A Quick Start

DZone's Guide to

Spring Boot: A Quick Start

Interested in giving Spring Boot a try? This guide provides a set of simple steps, commands, and annotations that will get a web app up and running quickly.

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

In this article, we will create a simple Spring Boot application which will run on embedded Apache Tomcat.

What Is Spring Boot?

Spring Boot helps in creating stand-alone, production-grade application easily with minimum fuss. It is the opinionated view of Spring framework and other third party libraries which believes in convenient configuration based setup.

Let's start building Spring Boot Application.

Adding Dependencies in Pom.xml

We will first add spring-boot-starter-parent as parent of our maven based project.

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.1.RELEASE</version>
</parent>


The benefit of adding spring-boot-starter-parent is that version managing of dependency is easy. You can omit the required version on the dependency. It will pick the one configured in the parent pom or from starters pom. Also, it conveniently setup the build related configurations as well.

Adding the Spring-Boot-Starter-Web Dependency

This will configure/add all the required dependencies for the spring-web module.

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


Writing the App Class

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


@SpringBootApplication
indicates that class is a configuration class and also triggers the auto-configuration through @EnableAutoConfiguration and component scanning through the @ComponentScan annotation in it.

@EnableAutoConfiguration

It enables the auto-configuration of Spring Application Context. It attempts to configure your application as per the classpath dependencies that you have added.

In the main() of the App class, we have delegated the call to the run() method of SpringApplication. SpringApplication will bootstrap and auto-configure our application and, in our case, will start the embedded Tomcat server. In the run method, we have passed App.class as an argument, which tells Spring that this is our primary Spring component (helps in bootstrapping).

Writing HelloGbController

 @RestController 
 public class HelloGbController { 
 @GetMapping 
    public String helloGb() { 
    return "Gaurav Bytes says, \"Hello There!!!\""; 
    } 
 } 


I have used two annotations — @RestController and @GetMapping. You can read more on new annotations introduced by Spring here.

@RestController signifies that this class is a web @Controller and Spring will consider it to handle incoming web requests.

Running the Application

You can use the maven command mvn spring-boot:run to run it as a Spring Boot application, and when you hit the localhost:8080, you will see the below web page.

You need to add the spring-boot-maven-plugin plugin to your build configuration in your pom.xml, then you can create a Jar with the maven command mvn repackage and simply run it as a JAR with the command java -jar spring-boot-quickstart-0.0.1-SNAPSHOT.jar.

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


This is how you can build a simple Spring Boot application. I hope you find this post helpful. You can download the example code from GitHub.

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
spring boot ,tutorial ,java ,web app development ,spring annotations

Published at DZone with permission of Gaurav Rai Mazra, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}