DZone
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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones Build AI Agents That Are Ready for Production
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
Build AI Agents That Are Ready for Production

"Platform Engineering & DevOps" Trend Report is now LIVE! Learn how internal platforms help developers ship faster with less friction

Join this live webinar to learn safer rollout techniques for schema changes, index testing, and database migrations.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Trending

  • If You Can Survive a Toddler, You Can Ship LLMs in Production
  • Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
  • Rust-Native Alternatives to Spark SQL and DataFrame Workloads
  • How to Build an Agentic AI SRE Co-Pilot for Incident Response
  1. DZone
  2. Coding
  3. Frameworks
  4. 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.

By 
Manoj Kumar Bardhan user avatar
Manoj Kumar Bardhan
·
Updated Jun. 18, 19 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
62.0K 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.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook