In this source code example, we will show you how to create a simple Spring boot Hello World REST API. We will use the Spring Boot 3 (the latest version) in this tutorial.
Technologies and tools used:
- Spring Boot 3.0.0 (SNAPSHOT)
- Maven 3.x
- Java 17 or later
Note:
Spring Boot 3 needs Java 17 or later
1. Creating a Spring Boot Application
Let's use https://start.spring.io/ to quickly create and bootstrap the Spring boot project:
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>3.0.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>net.javaguides</groupId>
<artifactId>springboot-hello-world-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-hello-world-example</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
3. Project Directory Structure
4. Spring Boot Hello World REST API
Let's create HelloWorldController and add the following content to it:
package net.javaguides.springboot;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1")
public class HelloWorldController {
@GetMapping("/hello-world")
public String helloWorld(){
return "Spring Boot Hello World! Example";
}
}
5. Run Spring Boot Application
We have successfully developed /hello-world REST API. Now it's time to deploy our application in a servlet container(embedded tomcat).
Two ways we can start the standalone Spring boot application.
1. From the root directory of the application and type the following command to run it -
$ mvn spring-boot:run
2. From your IDE, runSpringbootHelloWorldExampleApplicatio.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/.
6. Demo
We use the Postman client tool to test the /hello-world REST API.
Spring Boot Source Code Examples
- @GetMapping Spring Boot Example
- @PostMapping Spring Boot Example
- @PutMapping Spring Boot Example
- @DeleteMapping Spring Boot Example
- @PatchMapping Spring Boot Example
- @SpringBootApplication - Spring Boot
- Spring Boot Hello World REST API Example
- Spring Boot REST API returns Java Bean
- Create Spring Boot REST API returns List
- Spring Boot REST API with Path Variable
- Spring Boot REST API with Request Param
- Spring Boot Hibernate MySQL CRUD REST API Tutorial
- Spring Boot Real-Time Project Development using Spring MVC + Spring Security + Thymeleaf and MySQL Database
- Spring Boot Tutorial - User Login and Registration Backend + Email Verification
- Spring Boot JUnit and Mockito Example - Service Layer Testing
- Spring Professional Certification Cost
- Spring Boot Validate JSON Request Body
- Spring Boot One to Many CRUD Example | REST Controller
- Spring Boot Project with Controller Layer + Service Layer + Repository/DAO Layer
- Spring Boot Reactive MongoDB CRUD Example - WebFlux
- Spring Boot Amazon S3 - File Upload Download Delete Example
- Spring Boot RabbitMQ Publisher and Consumer Example
- Free Spring Boot Open Source Projects for Learning Purpose
- Spring Boot + Microsoft SQL Server + Hibernate Example
- Spring Boot Hibernate Thymeleaf MySQL CRUD Example
- Spring Boot CRUD Example with Spring MVC, Spring Data JPA, ThymeLeaf, Hibernate, MySQL
- Spring Boot Hibernate RESTful GET POST PUT and DELETE API Tutorial
- Best YouTube Channels to learn Spring Boot
- React Spring Boot Example
- Spring Boot Groovy Thymeleaf Example Tutorial
- Spring Boot Scala Thymeleaf Example Tutorial
- Spring Boot Hibernate DAO with MySQL Database Example
- Spring Boot PostgreSQL CRUD Example
- Spring Boot CRUD Example with MySQL
- Spring Boot Starter Parent
- Spring Boot JdbcTemplate Example
- Spring Boot PayPal Payment Gateway Integration Example
- Create Spring Boot REST API
Comments
Post a Comment