The Spring framework is an open-source application framework for the Java programming language. It provides a set of classes and libraries to build flexible and scalable enterprise grade web applications.
Spring boot is a module or an extension built on top of the Spring framework to speed up the development process of building Spring applications.
For a complete description of Spring boot and how its relate to the Spring framework, refer to the links above or to this article “What is Java Spring Boot?” by Microsoft.
Building a REST API for a ToDo app
We’re going to build a simple ToDo list API using Spring boot, which is an extension of the Spring framework. The API will have three endpoints:
- HTTP GET method that returns a todo item by its Id.
GET /todos/{id}
- HTTP POST method that creates a new todo item.
POST /todos/
- HTTP DELETE method that deletes a todo item by its Id.
DELETE /todos/{id}
Step 1: Generate the starter project using Spring Initializr
Go to https://start.spring.io. This service allows you to add all the dependencies you need for an application to start up your Spring Boot project.
Choose Maven and Java and complete the project metadata.
Click Add Dependencies and select
Spring Web
.Click Generate to download the resulting ZIP file, which is an archive of the Spring web application.
Step 2: Import the project into your IDE and write the code
- Unzip the downloaded project and open it up in Intellij IDEA.
- Open the
pom.xml
file. If you see an error message that says thespring-boot-maven-plugin
can not be found, then add the version to this plugin as highlighted below:
- Open the main class under
src/main/java/<your-project-id>
and add the following to the main method:
- Create the Controller class
TodoController.java
next to the main class (undersrc/main/java/<your-project-id>
) - Add the API end points to the controller as shown below.
- Note that this class will make use of features such as streams and collection, which are available in Java 8 and above, to find, add, and delete a ToDo item.
|
|
- Create a class for the ToDo item named
ToDo.java
import java.util.UUID;
public class ToDo {
private String name;
private String id;
private boolean isComplete;
public ToDo(){
this.isComplete = false;
this.id = UUID.randomUUID().toString();
}
public ToDo(String name) {
this.name = name;
this.isComplete = false;
this.id = UUID.randomUUID().toString();
}
public String getId() {
return this.id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public boolean isComplete() {
return this.isComplete;
}
public void setComplete(boolean isComplete) {
this.isComplete = isComplete;
}
public String toString() {
return "ToDo: id: " + this.id + "\nName: " + this.name + "\nDone: " + (this.isComplete ? "Yes" : "No");
}
}
Running the application
You can run the application from either the IDE or the Terminal.
Running the app from the Intellij IDEA IDE
- Go to the maven window
- Expand the project lifecycle and select package.
- Click on Run
- Run the jar file from the Terminal window in Intellij using
java -jar target/<your-project-id-version.jar>
Running the app from the Terminal
- On the Windows environment, open your Terminal application,
cd
to the project directory and run:
mvnw.cmd spring-boot:run
- On macOS or Linux environment, open your Terminal application,
cd
to the project directory and run:
./mvnw spring-boot:run
Step 4: Using the API
You may use the command line tool curl
to send requests to the API or use a desktop HTTP client app like Postman.
Using the command line tool curl:
- Add a todo list item
curl -X POST "http://localhost:8080/todos/" -H 'Content-Type: application/json' -d '{"name":"Buy Milk"}'
true
- Find a todo list Item
curl http://localhost:8080/todos/5a3c41e0-0fcb-439e-91f1-3c9b0862b016
{"name":"Buy Milk","id":"5a3c41e0-0fcb-439e-91f1-3c9b0862b016","complete":false}
- Delete a todo item
curl -X DELETE http://localhost:8080/todos/5a3c41e0-0fcb-439e-91f1-3c9b0862b016
true
Using Postman
Add a todo list item
Find a todo list Item
Delete a todo item