Blog
Create a Simple Rest API in Java
Create a Simple Rest API in Java
A simple REST API in Java using Spring Boot â the most popular framework for building RESTful APIs in Java.
Simple REST API in Java with Spring Boot
Spring Boot is a Java-based framework used to create standalone, production-ready, and easy-to-deploy Spring applications. It simplifies the process of building web applications and microservices with minimal configuration.
Tools Needed
-
Java 17 or later
-
Maven
-
Spring Boot
-
Any IDE (Eclipse, IntelliJ, VS Code)
Project Structure
simple-rest-api/ │ ├── src/ │ └── main/ │ ├── java/ │ │ └── com/ │ │ └── example/ │ │ └── demo/ │ │ ├── DemoApplication.java │ │ │ │ │ └── controller/ │ │ └── HelloController.java │ │ │ └── resources/ │ └── application.properties │ └── pom.xml
Project File and Folder Description
| File / Folder | Purpose |
|---|---|
DemoApplication.java |
Main Spring Boot application class. |
controller/ |
Contains REST controller classes. |
HelloController.java |
Handles HTTP requests and returns API responses. |
application.properties |
Configures application settings, such as server port. |
pom.xml |
Manages Maven dependencies, plugins, and project configuration. |
pom.xml
<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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>simple-rest-api</name>
<description>Simple REST API in Java using Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Starter Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Spring Boot Maven Plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
DemoApplication.java
package com.testingdocs.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
HelloController.java
package com.testingdocs.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Welcome to Simple REST API in Java!";
}
}
application.properties
# Use default port 8080
How to Run
Use the terminal or your IDE and run:
mvn spring-boot:run
Test the API
Open your browser or use Postman/curl:
GET http://localhost:8080/api/hello
Response
Hello, Welcome to Simple REST API in Java!