Boot

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.

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 Eclipse Project
Create Gradle Project

Select “Gradle Project”:

Create Gradle Project
Create Gradle Project

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

CP3_WM
Create Gradle Project – Welcome Page

Enter the name of your project.

Enter Name of Gradle Project
Enter Name of Gradle Project

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

CP5_WM
Create Gradle Project – Gradle Wrapper

Press finish on the preview screen.

Finish Screen
Finish Screen

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

Gradle Project Structure
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
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
Create Sample Application – Step 1

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

Create Sample Application - Step 2
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
Create Sample Application – Step 3

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

SampleApplication.java
SampleApplication.java

At the command prompt run:

 gradle clean build

As shown below the build is now successful.

Gradle Initial Build Success
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.
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 Sample Application - Step 1
Create SampleController.java – Step 1

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

Create Sample Application - Step 2
Create SampleController.java – Step 2

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

Create SampleController.java - Final step
Create SampleController.java – Final step

Following is the current snapshot of the project structure.

Project Structure Snapshot
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
Gradle bootRun output

When invoked from browser the output is displayed as below.

Browser Output
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

Srujana Cherukuri

Srujana holds Master of Computer Applications degree from Osmania University, Hyberabad, India. She is currently working as a Java/J2EE Consultant and has experience of working with a number of clients in a variety of domains.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button