Spring Boot Hello World REST API Example

In this post, we will build a simple hello world REST API using Spring boot.

Spring Boot is an opinionated framework that helps developers build Spring-based applications quickly and easily. The main goal of Spring Boot is to quickly create Spring-based applications without requiring developers to write the same boilerplate configuration again and again.

Let's create a Spring boot application step by step.

1. Create Spring Boot Project

Spring Boot provides a web tool called https://start.spring.io to bootstrap an application quickly. Just go to https://start.spring.io and generate a new spring boot project.

Use the below details in the Spring boot creation:

Project Name: springboot-first-app

Project Type: Maven

Choose dependencies: Spring Web

Package name: com.springboot.app

2. Maven Dependencies

<?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 https://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.5.0</version>	<relativePath/> <!-- lookup parent from repository -->	</parent>	<groupId>com.springboot.app</groupId>	<artifactId>springboot-first-app</artifactId>	<version>0.0.1-SNAPSHOT</version>	<name>springboot-first-app</name>	<description>Spring Boot First Application</description>	<properties>	<java.version>16</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>	</dependency>	</dependencies>	<build>	<plugins>	<plugin>	<groupId>org.springframework.boot</groupId>	<artifactId>spring-boot-maven-plugin</artifactId>	</plugin>	</plugins>	</build> </project>

3. Spring Boot Hello World REST API

Let's create a HelloWorldController class and the below code to it:
package com.springboot.first.app; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController {	// GET HTTP Method	// http://localhost:8080/hello-world	@GetMapping("/hello-world")	public String helloWorld() {	return "Hello World!";	} }
  • The above code uses Spring 4’s new @RestController annotation, which marks the class as a controller where every method returns a domain object instead of a view. It’s shorthand for @Controller and @ResponseBody rolled together.
  • @GetMapping annotation for mapping HTTP GET requests onto specific handler methods. Specifically, @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).

4. Run Spring Boot Application

The below class SpringbootFirstAppApplication is the entry point that sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning.

package com.springboot.first.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootFirstAppApplication {	public static void main(String[] args) {	SpringApplication.run(SpringbootFirstAppApplication.class, args);	} }

Run spring boot application from the IDE:

From your IDE, run the SpringbootFirstAppApplication.main() method as a standalone Java class that will start the embedded Tomcat server on port 8080 and point the browser to http://localhost:8080/.

Run spring boot application using the command line:

Just go to the root directory of the application and type the following command to run it -

$ mvn spring-boot:run

The application will start at Spring Boot’s default tomcat port 8080.

Just hit this link in a browser: http://localhost:8080/hello-world. You will able to see the response of this REST API in the browser.


Comments