Tutorials / Tech blog / Geek Zone

Latest Posts

Spring Boot :: A Quick Introduction

Spring Boot is Spring's convention-over-configuration solution. It helps in creating stand-alone, production grade Spring based applications that you can just run with minial fuss. It is the opinionated view of Spring framework and other third party libraries.


Features

  • Create stand-alone Spring applications
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  • Provide opinionated 'starter' POMs to simplify your Maven configuration
  • Automatically configure Spring whenever possible
  • Provide production-ready features such as metrics, health checks and externalized configuration
  • Absolutely no code generation and no requirement for XML configuration

Lets start building our first Spring Boot application. I am using STS-3.9.0 IDE for this tutorial.

Create a Spring Starter Project by navigating File -> New -> Spring Starter Project (later select Web and provide other relevant details and press Finish.

(or press Alt+Shift+N and then select Spring Starter Project from the list)




This will create a Spring Starter Web project with the following pom.xml and [Project Name ] + Application.java  (Note :: The class will be annotated as @SpringBootApplication )

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


Example -> pom.xml


1
2
3
4
5
6
<parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.5.6.RELEASE</version>
 <relativePath /> <!-- lookup parent from repository -->
</parent>

The above dependency takes care of all dependencies required for spring web module.

Example --> Application.java

1
2
3
4
5
6
7
@SpringBootApplication
public class SpringBootQuickStartApplication {

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

In the above main() method we have invoked the run() method of SpringApplication. This will bootstrap and auto-configure our application to start via embedded Tomcat server (for our tutorial). SpringBootQuickApplication.class is passed as an argument here to help Spring understand that this class is our core component to kickstart.

We have also created GreetingController controller 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@RestController
@RequestMapping("greeting")
public class GreetingController {

 @GetMapping(produces = MediaType.TEXT_PLAIN_VALUE,path = "/hello")
 public String helloUser() {

  return "Hello User !!";
 }
}


Spring 4.0 introduced @RestController which is a stereotype annotation that combines @ResponseBody and @Controller

Also, @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET). We will be using this annotation for the following tutorials.

Running this project as a standalone JAVA class will start the embedded Tomcat server on default port 8080. This port is configurable and can be changed via application.properties file. Please refer here for more details. 



Project Structure



Hope you found the above tutorial helpful. Kindly refer Github for entire source code.

https://github.com/g33kzone/SpringBootQuickStart

No comments:

Post a Comment