Spring Boot REST hello world Example
Spring Boot Overview
Spring Boot is open source framework developed by Pivotal , used to build production ready standalone Spring application.
Spring Boot makes it easy to create Spring-based applications very quickly for production ready microservices that you can "just run". Let explore it's main key components as given below.
Spring Boot Components- Spring Boot Starters
- Spring Boot AutoConfigurator
- Spring Boot CLI
- Spring Boot Actuator
- Spring STS Suite (STS)
- Eclipse IDE
- IntelliJ IDEA
Spring Boot Starters
Spring Boot Starter's primary responsibility is to consolidate a set of specific or similar dependencies into single dependencies. The Spring Boot Starter module bundles all similar jar into a single jar.
For example, "spring-boot-starter-web" would have all related jars into one single jar. So there is no need to add all dependencies/jars required to build web application. Once we put "spring-boot-starter-web" jar file dependency to our pom.xml, Spring Boot Framework can install the necessary jars immediately and link them to our project classpath.
Add below dependency in pom.xml for this feature to get enable.<!-- Parents dependency is required to manage child dependencies versions-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath />
</parent>
<!-- Spring web starter will add all required dependencies for web application. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Spring Boot AutoConfigurator
Spring Boot AutoConfigurator's main responsibility is to reduce the spring configuration. By using Spring Boot AutoConfigurator annotation, we do not need to specify a XML configuration and essentially no or minimum annotation configuration for designing Spring applications in Spring boot.
For Example, if we want develop Spring MVC using only Spring Framework, the we have to define lot's of configurations in XML file.
Here in Spring Boot, using
@SpringBootApplication
annotation at class level at top, it will automatically
add all below annotations to Java Class ByteCode.
@Configuration
@EnableAutoConfiguration
@ComponentScan
Spring Boot CLI
Spring Boot CLI provide a way to run, test your application using Command Line Interface (CLI). When using CLI, Spring boot autoconfigures components internally to resolve all dependencies and execute the application and it runs Groovy scripts.
spring run NotesApp.groovy
Spring Boot Actuator
Spring Boot has a number of additional functionality that help you to track your application to manage and monitor by using Spring Boot Actuator feature.
It provides many benefits like track/monitor our request, obtain statistics/metrics.
To enable this feature, add below dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Now let's gets start to create Spring Boot App with rest api using below steps.
Create Spring Boot Rest Sample Example
- Go to Spring on official site (https://start.spring.io/).
- Select Maven project, add dependencies
- Click on Generate the project Button.
- Save and extract the project.
- Open IDE and import the project and click on browse and select the extracted project folder where pom.xml resides.
- Once project gets imported, follow below steps to create controller.
- Project Structure
- It will generate Main Class as given below with SpringBootApplication annotation.
package com.techgeeknext.emp.task.EmpTask;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EmpTaskApplication {
public static void main(String[] args) {
SpringApplication.run(EmpTaskApplication.class, args);
}
}
package com.techgeeknext.emp.task.EmpTask.controller;
import com.techgeeknext.emp.task.EmpTask.model.Employee;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EmployeeController {
@RequestMapping(value = "/get/employees", method = RequestMethod.GET)
public String getEmployees() {
final Employee employee = new Employee("Tech Geek Next Employee");
return "Hello "+ employee.getName();
}
}
package com.techgeeknext.emp.task.EmpTask.model;
public class Employee {
private String name;
public Employee(String name)
{
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}