Steps to Create Hello World Example in Spring Boot

Step 1: In Eclipse, File > New > Maven Project

Step 2: Select an Archetype, maven-archetype-quickstart

Step 3: Fill up the Group Id, Artifact Id 

Clicking on “Finish” will create a new project in Eclipse.

Step 4: We are going to add <parent> tag, <dependencies> tag, <build> tag, <repositories> tag, <pluginRepositories> tag as below in pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.0.M4</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.talksinfo</groupId>
  <artifactId>helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>helloworld</name>
  <description>Demo project for Spring Boot</description>

  <properties>
    <java.version>1.8</java.version>
  </properties>

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

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
        <exclusion>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

  <repositories>
    <repository>
      <id>spring-snapshots</id>
      <name>Spring Snapshots</name>
      <url>https://repo.spring.io/snapshot</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
    <repository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/milestone</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>spring-snapshots</id>
      <name>Spring Snapshots</name>
      <url>https://repo.spring.io/snapshot</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </pluginRepository>
    <pluginRepository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/milestone</url>
    </pluginRepository>
  </pluginRepositories>

</project>

Step 5: Create a class HelloWorldApplication.java

The SpringApplication class provides a convenient way to bootstrap a Spring application that is started from a main() method.  Static SpringApplication.run method starts the spring boot application.

package com.talksinfo.helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloworldApplication {

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

}

Step 6: Create a class HelloWorld.java

package com.talksinfo.helloworld.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorld {

  @RequestMapping("/sayHello")
  public String sayHello(){
    return "<h1>Hello World!!!<h1>";
  }
}

Step 7: Right Click on project and Run as SpringBoot App (in my case I have selected debug as)

Which would result in something as below in console.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::             (v2.2.0.M4)

2019-06-28 01:55:20.573  INFO 11912 --- [           main] c.t.helloworld.HelloworldApplication     : Starting HelloworldApplication on LAPTOP-D2GVO184 with PID 11912 (E:\Workspace\BlogWorkspace\SpringBoot\helloworld\target\classes started by vrdha in E:\Workspace\BlogWorkspace\SpringBoot\helloworld)
2019-06-28 01:55:20.575  INFO 11912 --- [           main] c.t.helloworld.HelloworldApplication     : No active profile set, falling back to default profiles: default
2019-06-28 01:55:21.170  INFO 11912 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-06-28 01:55:21.176  INFO 11912 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-06-28 01:55:21.177  INFO 11912 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-06-28 01:55:21.238  INFO 11912 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-06-28 01:55:21.238  INFO 11912 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 621 ms
2019-06-28 01:55:21.361  INFO 11912 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-06-28 01:55:21.458  INFO 11912 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-06-28 01:55:21.460  INFO 11912 --- [           main] c.t.helloworld.HelloworldApplication     : Started HelloworldApplication in 1.098 seconds (JVM running for 1.635)

Step 8: Open http://localhost:8080/sayHello in browser, which would display as below.

Conclusion:

Hence we saw how to create helloworld example using spring boot and run as micro-service.

Please follow and like us: