Spring Boot

Spring Boot makes it easy to create stand-alone enterprise application. So far we had to include many maven dependencies to include libraries jars for Spring core, Spring AOP, Spring MVC, Servlets, JUnit, Log 4J etc. All these libraries have different versions that sometimes do not integrate when we include them. We also have to have our web server like Tomcat and Jetty and deploy our application on server and run them.

Spring Boot remove all these dependencies problems and having separate tomcat/jetty server. It provides you a simple configuration environment with integrated web servers to run your spring application. Single class will run your entire application with integrated web server.

For example if you want to make a simple REST controller in Spring MVC and run it then it is very simple to build in Spring Boot and run it.

Assumed that here is your REST controller

package com.sunilos.springboot;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;

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

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

/**

* User rest controller

*/

@RestController

@RequestMapping("/User")

public class UserCtl {

/**

* Mapped GET http method

*/

@GetMapping()

public String display() {

return "Hello User by Get!";

}

/**

* Mapped Post http method

*/

@PostMapping()

public String submit() {

return "Hello User by Post!";

}

}

This controller you want to build in Eclipse using Maven project and run it. You need to follow following simple steps to do this:

Step: 1 Create a simple maven project

Step: 2 Make following entries in pom.xml file

<!-- Parent contains required compatible dependency version details -->

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.7.RELEASE</version>

</parent>

<dependencies>

<!-- Loads dependent jars for Spring Web from parent configuration -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!-- Loads dependent jars for Spring JUnit test from parent configuration -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

<!-- Set JDK Vertion to 1.8 -->

<properties>

<java.version>1.8</java.version>

</properties>

Step: 3 create UserCtl class src/main/java folder

Step: 4 create BootApp class to starts your spring boot environment with tomcat server

package com.sunilos.springboot;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

* Starts spring boot application. Annotation @SpringBootApplication is used to

* configure container and scan @Component, @Controller, @Servive and @Repsitory

* beans

*

* @author Sunil Sahu

*

*/

@SpringBootApplication

public class BootApp {

public static void main(String[] args) throws Exception {

/// Pass class of main method as parameter to SpringApplication.run

SpringApplication.run(BootApp.class, args);

}

}

Step: 5 Run this BootApp class as java application

You will see it will automatically start your tomcat server and auto scan controller and deploy it.

Step: 6 Test your controller

You can test your controller from browser using http://localhost:8080/User url. You will get response.

That's it congratulation you have learned Spring Boot and ready to deploy your application on Spring Boot. Sample code are attached at bottom of this page.

References

https://projects.spring.io/spring-boot/#quick-start

References

Q: How do you start Spring Boot application ?

Q: Which kinds of applications are developed in SpringBoot?

A: Standalone applications are developed in SpringBoot.

Q: Do you need to install Tomcat Server for SpringBoot?

A: No, It is in-build do not need to install Tomcat separately .

Q: Which maven dependencies you will include?

A: ??

Q: What is the advantage of SpringBoot ?

A: ??

Q: How do you create controllers in SpringBoot?

A: We create controllers using @RestController annotation.

Q: How do you create REST Web Services in SpringBoot?

A: Using rest controllers

Q: Which data is send and receive by Rest controllers?

A: JSON data is send and receive by Rest controllers.

Q: What is the use of @RequestBody annotation?

A: It receives JSON object from request body when data is submitted by POST method.

Q: What is the use of @ResponseBody annotation?

A: It sends JSON object in response body

Q: Write code to start Spring Boot application.