Sunday 8 July 2018

Spring boot: Hello World Application

Below step-by-step procedure explains how to develop and run simple application using spring boot.

Requirements: Eclipse

Step 1: Open Eclipse, and create new maven project.


Select the check box 'Create a simple project (skip archetype selection)'



Click on Next button.





Give the Group Id as 'org.selflearning', artifact id as 'springDemo' and version as 1.

Click on Finish button.

Project is created like below.


Step 2:  Add the spring boot dependencies to pom.xml.


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Total pom looks like below.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>org.selflearning</groupId>
 <artifactId>springDemo</artifactId>
 <version>1</version>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.1.RELEASE</version>
 </parent>

 <dependencies>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

 </dependencies>

</project>

Step 3: Create a package 'com.sample.demo' and define the class HomePageController like below.


HomePageController.java
package com.sample.demo;

import org.springframework.boot.SpringApplication;
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;

@Controller
@EnableAutoConfiguration
public class HomePageController {
 @RequestMapping("/")
 @ResponseBody
 String home() {
  return "Hello World!";
 }

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


Run above application and hit the url ‘http://localhost:8080/’ in browser, you can able to see ‘Hello World!’ message.

SpringApplication.run(HomePageController.class, args);
Run the SpringApplication from the specified source using default settings.

@Controller
If you annotate any class with this annotation, then Spring considers it when handling incoming web requests.

@RequestMapping
It maps the url to specific function.

@EnableAutoConfiguration
This annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added.



Previous                                                 Next                                                 Home

No comments:

Post a Comment