DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Spring Boot Tutorial for Beginners: Hello World Program

Spring Boot Tutorial for Beginners: Hello World Program

If you're new to Spring Boot or need a refresher, read on to learn how to create a hello world app and run it on a Tomcat server.

Manoj Kumar Bardhan user avatar by
Manoj Kumar Bardhan
·
Jun. 18, 19 · Web Dev Zone · Tutorial
Like (6)
Save
Tweet
58.82K Views

Join the DZone community and get the full member experience.

Join For Free

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." It's basically used to minimize the configuration or the boiler plate code.

In this example, we have used the below frameworks and tools.

  • Maven 3.3.9
  • JDK 1.8
  • Eclipse IDE
  • spring-boot dependency

First step - In Eclipse, create a Maven project called "hello-world-spring-boot" as shown below:

Hello World Spring Boot File

Then add the dependency for Spring Boot and plug it into the pom.xml file.

<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>com.javadevelopersguide.www</groupId>
    <artifactId>hello-world-spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <description>This is a hello world example with Spring Boot.</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Then create a controller class called HelloWorldController with a REST API method,  sayHello().

HelloWorldController.java

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
 *
 */
@Controller
@EnableAutoConfiguration
public class HelloWorldController {
@RequestMapping("/hello")
@ResponseBody
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.
  • @EnableAutoConfiguration - This enables auto configuration for the Application Context.
  • @RequestMapping - This is used to map to the Spring MVC controller method.
  • @ResponseBody - Used to bind the HTTP response body with a domain object in the return type. This annotation works behind the scenes.

Now, our controller is ready. We just need a luncher that can lunch our Spring Boot application. We need to create a "SpringBootApplicationLuncher" file.

SpringBootApplicationLuncher.java

package com.javadevelopersguide.springboot.example;

import org.springframework.boot.SpringApplication;

/**
 * This Luncher for the spring boot application.
 * 
 * @author manoj.bardhan
 *
 */
public class SpringBootApplicationLuncher {
public static void main(String[] args) {
SpringApplication.run(HelloWorldController.class, args);
}
}

Now we can run this launcher to start the Spring Boot application. As we know, Spring Boot is embedded with Tomcat feature. Now, the application is up and running.

Try this Tomcat URL, which is running on http://localhost:8080/hello.


Tomcat server up and running

Alternatively, you can also start your Spring Boot application via the command line (Terminal/Console). Here in this example we have used windows OS.

Below is the 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


Command terminal

Tomcat Started

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

URL  - http://localhost:8080/hello

Spring Framework Spring Boot

Published at DZone with permission of Manoj Kumar Bardhan. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Evaluate Software Quality Assurance Success: KPIs, SLAs, Release Cycles, and Costs
  • Monolith vs Microservices Architecture: To Split or Not to Split?
  • Major PostgreSQL Features You Should Know About
  • Are All Kubernetes Ingresses the Same?

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo