Thursday, 15 March 2018

Spring Boot Hello World Program

Developing your first Spring Boot application is quite easy. As we know Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". Its basically to minimize the configuration. 

In this example I have used below frameworks and tools for this example.

1. Maven 3.3.9 
2. JDK 1.8
3. Eclipse IDE
4. spring-boot dependency 

First step - In eclipse create a maven project  "hello-world-spring-boot" as below .

Then add the dependency for spring-boot and plug-in in the pom.xml file.


<project xmlns="" xmlns:xsi=""
<description>This is a hello world example with Spring Boot.</description>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>

Then create a controller class "HelloWorldController" with a rest api method sayHello()
package com.javadevelopersguide.springboot.example;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 * @author manoj.bardhan
public class HelloWorldController {
public String sayHello() {
return "Hello World Developer!!!";
I have use below annotations in my controller. Here in this example the uri path is /hello

@Controller - This is used to specify the controller , as its spring framework basic.
@EnableAutoConfiguration - This enable auto configuration for Application Context. 
@RequestMapping - This is used to map to spring mvc controller method.
@ResponseBody - Used to bind http response body with a domain object in return type.Its behind the scenes. 

Now , my controller is ready.Just I need a luncher , who can lunch my spring boot application. I have created a "SpringBootApplicationLuncher".

package com.javadevelopersguide.springboot.example;
import org.springframework.boot.SpringApplication;
 * This Luncher for my spring boot application.
 * @author manoj.bardhan
public class SpringBootApplicationLuncher {
public static void main(String[] args) {, args);

Now you can run this luncher to start the spring boot application.Then, you can see the below screenshot showing the tomcat is started. As you know spring-boot is embedded with tomcat feature.

Now , your application is up and running . I have highlighted above that the tomcat is started on default port 8080

Try this tomcat URL, which is running now :- http://localhost:8080/hello

Alternatively , Also you can also start your spring-boot application on command line (Terminal). I have used windows OS.

You can use the below Maven Command  to build and run this spring-boot application :- 

1. Build the application :- mvn clean install

2. Run the application :- mvn spring-boot:run

 Now the service is running on tomcat port 8080 .Use the below URL to access the sayHello() api.


Hope it will help you.

No comments:

Post a Comment