Spring Boot Hello World Example

In this example, You will learn about Introduction about Spring Boot Hello World example.

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. Most Spring Boot applications need very little Spring configuration.

You can use Spring Boot to create Java applications that can be started by using java -jar or more traditional war deployments. We can use command line tool to run the spring scripts.

Our primary goals are:

  • Provide a radically faster and widely accessible getting-started experience for all `spring development.
  • Be opinionated out of the box but get out of the way quickly as requirements start to diverge from the defaults.
  • Provide a range of non-functional features that are common to large classes of projects (such as embedded servers, security, metrics, health checks, and externalized configuration).

Features

  • Create stand-alone Spring applications.
  • No need to deploy the War file. Spring boot is embedded with Tomcat, Jetty or Undertow directly.
  • Provide opinionated ‘starter’ dependencies to simplify your build configuration.
  • Automatically configure Spring and 3rd party libraries whenever possible Provide production-ready features such as metrics, health checks, and externalized configuration.
  • Absolutely no code generation and no requirement for XML configuration.

Developing Your First Spring Boot Application

This section describes how to develop a simple “Hello World!” web application that highlights some of Spring Boot’s key features. We use Maven to build this project since most IDEs support it.

  • Create Spring boot hello world Example by using Spring INITIALIZR.

https://start.spring.io/

Spring Boot Hello world Example
Spring Boot Hello World Example
  • Download and import the maven project
  • Project Directory:

  • Pom Dependencies:

POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. 

  • HelloworldApplication.java

The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes, as shown in the following below example.

Many Spring Boot developers like their apps to use auto-configuration, component scan and be able to define extra configuration on their “application class”. @SpringBootApplication annotation can be used to enable those three features, that is:

  • @EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism.
  • @ComponentScan: enable @Component scan on the package where the application is located (see the best practices).
  • @Configuration: allow to register extra beans in the context or import additional configuration classes.
  • Let us create Controller for request mapping:

The first annotation in our Example class is @RestController. This is known as a stereotype annotation. It provides hints for people reading the code and for Spring that the class plays a specific role. In this case, our class is a web @Controller, so Spring considers it when handling incoming web requests.

The @RequestMapping annotation provides “routing” information. It tells Spring that any HTTP request with the / path should be mapped to the home method. The @RestController annotation tells Spring to render the resulting string directly back to the caller.

  • Output:
Spring Boot Hello world example
Spring Boot Hello world example
  • Download source code from GitHub:

https://github.com/praveen41/SpringBootHelloWorldExample

Related articles:

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons