Spring Boot Tutorial for beginners

1. Introduction

When I just heard about Spring Boot there were many questions to pop out of my head “What is it? Why do I need it? How different is it to the other features under the same Spring umbrella?” etc. I am sure you would have had similar inquisitions too.

In short, Spring Boot takes care of application infrastructure while you can focus on coding the actual business flesh. Surely fast tracks building of applications. It makes reasonable assumptions of the dependencies and adds them accordingly. It also lets you customize the dependencies according to your requirement.

In the following sections, I am going to site a sample application example using Spring Boot.

Want to master Spring Framework ?
Subscribe to our newsletter and download the Spring Framework Cookbook right now!
In order to help you master the leading and innovative Java framework, we have compiled a kick-ass guide with all its major features and use cases! Besides studying them online you may download the eBook in PDF format!

Thank you!

We will contact you soon.

2. Environment

This tutorial assumes that you have basic understanding of Gradle build framework and also that your Eclipse IDE (Luna) environment is fully setup and configured with:

In addition to the above you need the following to work on this sample application.

This tutorial assumes that you have basic understanding of Spring framework.

3. Sample Application using Spring Boot

3.1. Create and configure a Gradle project in Eclipse IDE

In the Eclipse IDE, click File -> New -> Other:

Create Gradle Project

Select “Gradle Project”:

Create Gradle Project

Take a moment to read the suggestions in the following screen. Press next.

Create Gradle Project – Welcome Page

Enter the name of your project.

Enter Name of Gradle Project

Keep the default and recommended Gradle Wrapper option selected and press next.

Create Gradle Project – Gradle Wrapper

Press finish on the preview screen.

Finish Screen

You have successfully created the Gradle project. The following is the project structure of your Gradle project.

Gradle Project Structure

3.2. build.gradle

3.2.1 Modify build.gradle

In the Eclipse IDE, open the build.gradle file that is in the project root directory. Modify the file as shown below.

build.gradle

 
buildscript {
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.2.RELEASE")
	}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
jar {
	baseName = 'sample'
	version =  '0.1.0'
}
repositories {
	mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
	compile("org.springframework.boot:spring-boot-starter-web")
}

3.2.2 Walk through build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.2.RELEASE")
    }
}
apply plugin:"spring-boot"

The buildscript() method is used to add any external libraries to script’s classpath, by passing in a closure that declares the build script classpath using classpath configuration.

Any binary plugins that have been published as external jars can be added to the project by adding them to the classpath configuration and then applying the plugin.

In our example, spring-boot-gradle-plugin is the binary plugin that needs to be added to our project. And apply plugin:"spring-boot" is used to apply the plugin.

Spring Boot Gradle plugin is added to project to provide Spring Boot support in Gradle.

Follow the link Spring Boot Gradle plugin to dig further about this plugin.

apply plugin: 'java'
apply plugin: 'eclipse'

The java plugin adds compilation, testing and bundling capabilities to the project. When build using the tasks from the eclipse plugin, certain files are added to project to enable it to get imported into Eclipse IDE.

jar {
	baseName = 'sample'
	version =  '0.1.0'
}

When the project is built with gradle clean build, the jar file with name sample-0.1.0.jar is created in the $PROJECT_ROOT/build/lib folder of the project.

repositories {
	mavenCentral()
}

This is to specify the repository where the dependencies will be downloaded from.

sourceCompatibility = 1.8
targetCompatibility = 1.8

The sourceCompatibility is Java version compatibility to use when compiling Java source. Default value is version of the current JVM in use. The targetCompatibility is Java version to generate classes for. The default value is sourceCompatibility.

dependencies {
	compile("org.springframework.boot:spring-boot-starter-web")
}

To specify the required dependency for Spring Boot.

3.2.3 Run initial build

At the command prompt run:

 gradle clean build

During build process, Spring Boot Gradle plugin looks for class with public static void main() to flag it as runnable class. As we haven’t created a class with public static void main() yet, the build fails as shown below.

Initial Build Failure

3.3. Create SampleApplication.java

Let’s create a simple runnable class with public static void main(). In Eclipse IDE, right click on source folder src/main/java and select New -> Other.

Create Sample Application – Step 1

Select “Class” in the resultant window. Click on “Next”:

Create Sample Application – Step 2

Specify the package name “sample”, class name “SampleApplication” and select to create public static void main(String[] args) method, as shown in the below picture. Click on “Finish”.

Create Sample Application – Step 3

After creating SampleApplication.java, the project structure looks as below.

SampleApplication.java

At the command prompt run:

 gradle clean build

As shown below the build is now successful.

Gradle Initial Build Success

As shown below Spring Boot makes reasonable assumptions of the dependencies and adds them automatically.

Project Dependencies added by Spring Boot.

3.4. Create SampleController.java

Lets us now create a simple controller SampleController.java

In Eclipse IDE, right click on source folder src/main/java and select New -> Other.

Create SampleController.java – Step 1

Select “Class” in the resultant window. Click on “Next”:

Create SampleController.java – Step 2

Specify the package name “sample”, class name “SampleController”. Click on “Finish”.

Create SampleController.java – Final step

Following is the current snapshot of the project structure.

Project Structure Snapshot

Modify SampleController.java as shown below.

SampleController.java

package sample;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SampleController {
	@RequestMapping("/sample")
	public String sampleIt(){
		return "Hello! Welcome to Spring Boot Sample. ";
	}
}
@RestController

The @RestController annotation marks the class as controller and adds @Controller and @ResponseBody annotations.

@RequestMapping

The @RequestMapping annotation ensures that HTTP requests to /sample is mapped to the sampleIt() method. As you would have already noticed, we didn’t have to create any web.xml for the mapping.

3.5. SampleApplication.java

3.5.1 Modify SampleApplication.java

SampleApplication.java

package sample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class SampleApplication {
	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(SampleApplication.class, args);
		System.out.println(ctx.getDisplayName());
		
		System.out.println("This is my first Spring Boot Example");
	}
}

Let’s explore through the SampleApplication.java:

@SpringBootApplication

If you are familiar with Spring framework, many-a-times you would have annotated your main class with @Configuration, @ComponentScan and @EnableAutoConfiguration. @SpringBootApplication is equivalent to using these three annotations @Configuration, @ComponentScan and @EnableAutoConfiguration with their default attributes.

To read further about these annotations, visit the links:

ApplicationContext ctx = SpringApplication.run(SampleApplication.class, args);

SpringApplication is used to bootstrap and launch a Spring application from the main method.

3.6 Run SampleApplication

At the command prompt:

 gradle clean build bootRun

The output is as follows.

Gradle bootRun output

When invoked from browser the output is displayed as below.

Browser Output

4. References

5. Conclusion

That’s all Folks!! Make sure you make yourself a cuppa before jumping into additional reading through the links provided in “References” section.

6. Download the Eclipse project

Download
You can download the full source code of this example here: Spring Boot Tutorial for Beginners
Exit mobile version