Creating a REST API with Spring Boot and MongoDB
This year I greeted Christmas in a different fashion: I was a part of the Java Advent Calendar. Let’s boot up for Christmas:
Spring Boot is an opinionated framework that simplifies the development of Spring applications. It frees us from the slavery of complex configuration files and helps us to create standalone Spring applications that don’t need an external servlet container.
This sounds almost too good to be true, but Spring Boot can really do all this.
This blog post demonstrates how easy it is to implement a REST API that provides CRUD operations for todo entries that are saved to MongoDB database.
Let’s start by creating our Maven project.
This blog post assumes that you have already installed the MongoDB database. If you haven’t done this, you can follow the instructions given in the blog post titled: Accessing Data with MongoDB.
Creating Our Maven Project
We can create our Maven project by following these steps:
- Use the spring-boot-starter-parent POM as the parent POM of our Maven project. This ensures that our project inherits sensible default settings from Spring Boot.
- Add the Spring Boot Maven Plugin to our project. This plugin allows us to package our application into an executable jar file, package it into a war archive, and run the application.
- Configure the dependencies of our project. We need to configure the following dependencies:
- The spring-boot-starter-web dependency provides the dependencies of a web application.
- The spring-data-mongodb dependency provides integration with the MongoDB document database.
- Enable the Java 8 Support of Spring Boot.
- Configure the main class of our application. This class is responsible of configuring and starting our application.
The relevant part of our pom.xml file looks as follows:
<properties> <!-- Enable Java 8 --> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- Configure the main class of our Spring Boot application --> <start-class>com.javaadvent.bootrest.TodoAppConfig</start-class> </properties> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.1.9.RELEASE</version> </parent> <dependencies> <!-- Get the dependencies of a web application --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Data MongoDB--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> </dependency> </dependencies> <build> <plugins> <!-- Spring Boot Maven Support --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
Additional Reading:
- Spring Boot Reference Manual: 9.1.1 Maven installation
- Spring Boot Reference Manual: 12.1 Maven
- Spring Boot Maven Plugin – Usage
Let’s move on and find out how we can configure our application.
Configuring Our Application
We can configure our Spring Boot application by following these steps:
- Create a TodoAppConfig class to the com.javaadvent.bootrest package.
- Enable Spring Boot auto-configuration.
- Configure the Spring container to scan components found from the child packages of the com.javaadvent.bootrest package.
- Add the main() method to the TodoAppConfig class and implement by running our application.
The source code of the TodoAppConfig class looks as follows:
package com.javaadvent.bootrest; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @EnableAutoConfiguration @ComponentScan public class TodoAppConfig { public static void main(String[] args) { SpringApplication.run(TodoAppConfig.class, args); } }
We have now created the configuration class that configures and runs our Spring Boot application. Because the MongoDB jars are found from the classpath, Spring Boot configures the MongoDB connection by using its default settings.
Additional Reading:
- Spring Boot Reference Manual: 13.2 Location the main application class
- Spring Boot Reference Manual: 14. Configuration classes
- The Javadoc of the @EnableAutoConfiguration annotation
- Spring Boot Reference Manual: 15. Auto-configuration
- The Javadoc of the SpringApplication class
- Spring Boot Reference Manual: 27.2.1 Connecting to a MongoDB database
Let’s move on and implement our REST API.
Implementing Our REST API
We need implement a REST API that provides CRUD operations for todo entries. The requirements of our REST API are:
- A POST request send to the url ‘/api/todo’ must create a new todo entry by using the information found from the request body and return the information of the created todo entry.
- A DELETE request send to the url ‘/api/todo/{id}’ must delete the todo entry whose id is found from the url and return the information of the deleted todo entry.
- A GET request send to the url ‘/api/todo’ must return all todo entries that are found from the database.
- A GET request send to the url ‘/api/todo/{id}’ must return the information of the todo entry whose id is found from the url.
- A PUT request send to the url ‘/api/todo/{id}’ must update the information of an existing todo entry by using the information found from the request body and return the information of the updated todo entry.
We can fulfill these requirements by following these steps:
- Create the entity that contains the information of a single todo entry.
- Create the repository that is used to save todo entries to MongoDB database and find todo entries from it.
- Create the service layer that is responsible of mapping DTOs into domain objects and vice versa. The purpose of our service layer is to isolate our domain model from the web layer.
- Create the controller class that processes HTTP requests and returns the correct response back to the client.
This example is so simple that we could just inject our repository to our controller. However, because this is not a viable strategy when we are implementing real-life applications, we will add a service layer between the web and repository layers.
Let’s get started.
Creating the Entity
We need to create the entity class that contains the information of a single todo entry. We can do this by following these steps:
- Add the id, description, and title fields to the created entity class. Configure the id field of the entity by annotating the id field with the @Id annotation.
- Specify the constants (MAX_LENGTH_DESCRIPTION and MAX_LENGTH_TITLE) that specify the maximum length of the description and title fields.
- Add a static builder class to the entity class. This class is used to create new Todo objects.
- Add an update() method to the entity class. This method simply updates the title and description of the entity if valid values are given as method parameters.
The source code of the Todo class looks as follows:
import org.springframework.data.annotation.Id; import static com.javaadvent.bootrest.util.PreCondition.isTrue; import static com.javaadvent.bootrest.util.PreCondition.notEmpty; import static com.javaadvent.bootrest.util.PreCondition.notNull; final class Todo { static final int MAX_LENGTH_DESCRIPTION = 500; static final int MAX_LENGTH_TITLE = 100; @Id private String id; private String description; private String title; public Todo() {} private Todo(Builder builder) { this.description = builder.description; this.title = builder.title; } static Builder getBuilder() { return new Builder(); } //Other getters are omitted public void update(String title, String description) { checkTitleAndDescription(title, description); this.title = title; this.description = description; } /** * We don't have to use the builder pattern here because the constructed * class has only two String fields. However, I use the builder pattern * in this example because it makes the code a bit easier to read. */ static class Builder { private String description; private String title; private Builder() {} Builder description(String description) { this.description = description; return this; } Builder title(String title) { this.title = title; return this; } Todo build() { Todo build = new Todo(this); build.checkTitleAndDescription(build.getTitle(), build.getDescription()); return build; } } private void checkTitleAndDescription(String title, String description) { notNull(title, "Title cannot be null"); notEmpty(title, "Title cannot be empty"); isTrue(title.length() <= MAX_LENGTH_TITLE, "Title cannot be longer than %d characters", MAX_LENGTH_TITLE ); if (description != null) { isTrue(description.length() <= MAX_LENGTH_DESCRIPTION, "Description cannot be longer than %d characters", MAX_LENGTH_DESCRIPTION ); } } }
Additional Reading:
Let’s move on and create the repository that communicates with the MongoDB database.
Creating the Repository
We have to create the repository interface that is used to save Todo objects to MondoDB database and retrieve Todo objects from it.
If we don’t want to use the Java 8 support of Spring Data, we could create our repository by creating an interface that extends the CrudRepository<T, ID> interface. However, because we want to use the Java 8 support, we have to follow these steps:
- Create an interface that extends the Repository<T, ID> interface.
- Add the following repository methods to the created interface:
- The void delete(Todo deleted) method deletes the todo entry that is given as a method parameter.
- The ListfindAll()method returns all todo entries that are found from the database.
- The OptionalfindOne(String id)method returns the information of a single todo entry. If no todo entry is found, this method returns an empty Optional.
- The Todo save(Todo saved) method saves a new todo entry to the database and returns the the saved todo entry.
The source code of the TodoRepository interface looks as follows:
import org.springframework.data.repository.Repository; import java.util.List; import java.util.Optional; interface TodoRepository extends Repository<Todo, String> { void delete(Todo deleted); List<Todo> findAll(); Optional<Todo> findOne(String id); Todo save(Todo saved); }
Additional Reading:
- The Javadoc of the CrudRepository<T, ID> interface
- The Javadoc of the Repository<T, ID> interface
- Spring Data MongoDB Reference Manual: 5. Working with Spring Data Repositories
- Spring Data MongoDB Reference Manual: 5.3.1 Fine-tuning repository definition
Let’s move on and create the service layer of our example application.
Creating the Service Layer
First, we have to create a service interface that provides CRUD operations for todo entries. The source code of the TodoService interface looks as follows:
import java.util.List; interface TodoService { TodoDTO create(TodoDTO todo); TodoDTO delete(String id); List<TodoDTO> findAll(); TodoDTO findById(String id); TodoDTO update(TodoDTO todo); }
The TodoDTO class is a DTO that contains the information of a single todo entry. We will talk more about it when we create the web layer of our example application.
Second, we have to implement the TodoService interface. We can do this by following these steps:
- Inject our repository to the service class by using constructor injection.
- Add a private Todo findTodoById(String id) method to the service class and implement it by either returning the found Todo object or throwing the TodoNotFoundException.
- Add a private TodoDTO convertToDTO(Todo model) method the service class and implement it by converting the Todo object into a TodoDTO object and returning the created object.
- Add a private ListconvertToDTOs(Listmodels)and implement it by converting the list of Todo objects into a list of TodoDTO objects and returning the created list.
- Implement the TodoDTO create(TodoDTO todo) method. This method creates a new Todo object, saves the created object to the MongoDB database, and returns the information of the created todo entry.
- Implement the TodoDTO delete(String id) method. This method finds the deleted Todo object, deletes it, and returns the information of the deleted todo entry. If no Todo object is found with the given id, this method throws the TodoNotFoundException.
- Implement the ListfindAll()method. This methods retrieves all Todo objects from the database, transforms them into a list of TodoDTO objects, and returns the created list.
- Implement the TodoDTO findById(String id) method. This method finds the Todo object from the database, converts it into a TodoDTO object, and returns the created TodoDTO object. If no todo entry is found, this method throws the TodoNotFoundException.
- Implement the TodoDTO update(TodoDTO todo) method. This method finds the updated Todo object from the database, updates its title and description, saves it, and returns the updated information. If the updated Todo object is not found, this method throws the TodoNotFoundException.
The source code of the MongoDBTodoService looks as follows:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; import static java.util.stream.Collectors.toList; @Service final class MongoDBTodoService implements TodoService { private final TodoRepository repository; @Autowired MongoDBTodoService(TodoRepository repository) { this.repository = repository; } @Override public TodoDTO create(TodoDTO todo) { Todo persisted = Todo.getBuilder() .title(todo.getTitle()) .description(todo.getDescription()) .build(); persisted = repository.save(persisted); return convertToDTO(persisted); } @Override public TodoDTO delete(String id) { Todo deleted = findTodoById(id); repository.delete(deleted); return convertToDTO(deleted); } @Override public List<tododto> findAll() { List<todo> todoEntries = repository.findAll(); return convertToDTOs(todoEntries); } private List<tododto> convertToDTOs(List<todo> models) { return models.stream() .map(this::convertToDTO) .collect(toList()); } @Override public TodoDTO findById(String id) { Todo found = findTodoById(id); return convertToDTO(found); } @Override public TodoDTO update(TodoDTO todo) { Todo updated = findTodoById(todo.getId()); updated.update(todo.getTitle(), todo.getDescription()); updated = repository.save(updated); return convertToDTO(updated); } private Todo findTodoById(String id) { Optional<todo> result = repository.findOne(id); return result.orElseThrow(() -> new TodoNotFoundException(id)); } private TodoDTO convertToDTO(Todo model) { TodoDTO dto = new TodoDTO(); dto.setId(model.getId()); dto.setTitle(model.getTitle()); dto.setDescription(model.getDescription()); return dto; } }
We have now created the service layer of our example application. Let’s move on and create the controller class.
Creating the Controller Class
First, we need to create the DTO class that contains the information of a single todo entry and specifies the validation rules that are used to ensure that only valid information can be saved to the database. The source code of the TodoDTO class looks as follows:
import org.hibernate.validator.constraints.NotEmpty; import javax.validation.constraints.Size; public final class TodoDTO { private String id; @Size(max = Todo.MAX_LENGTH_DESCRIPTION) private String description; @NotEmpty @Size(max = Todo.MAX_LENGTH_TITLE) private String title; //Constructor, getters, and setters are omitted }
Additional Reading:
Second, we have to create the controller class that processes the HTTP requests send to our REST API and sends the correct response back to the client. We can do this by following these steps:
- Inject our service to our controller by using constructor injection.
- Add a create() method to our controller and implement it by following these steps:
- Read the information of the created todo entry from the request body.
- Validate the information of the created todo entry.
- Create a new todo entry and return the created todo entry. Set the response status to 201.
- Implement the delete() method by delegating the id of the deleted todo entry forward to our service and return the deleted todo entry.
- Implement the findAll() method by finding the todo entries from the database and returning the found todo entries.
- Implement the findById() method by finding the todo entry from the database and returning the found todo entry.
- Implement the update() method by following these steps:
- Read the information of the updated todo entry from the request body.
- Validate the information of the updated todo entry.
- Update the information of the todo entry and return the updated todo entry.
- Create an @ExceptionHandler method that sets the response status to 404 if the todo entry was not found (TodoNotFoundException was thrown).
The source code of the TodoController class looks as follows:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import javax.validation.Valid; import java.util.List; @RestController @RequestMapping("/api/todo") final class TodoController { private final TodoService service; @Autowired TodoController(TodoService service) { this.service = service; } @RequestMapping(method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) TodoDTO create(@RequestBody @Valid TodoDTO todoEntry) { return service.create(todoEntry); } @RequestMapping(value = "{id}", method = RequestMethod.DELETE) TodoDTO delete(@PathVariable("id") String id) { return service.delete(id); } @RequestMapping(method = RequestMethod.GET) List<TodoDTO> findAll() { return service.findAll(); } @RequestMapping(value = "{id}", method = RequestMethod.GET) TodoDTO findById(@PathVariable("id") String id) { return service.findById(id); } @RequestMapping(value = "{id}", method = RequestMethod.PUT) TodoDTO update(@RequestBody @Valid TodoDTO todoEntry) { return service.update(todoEntry); } @ExceptionHandler @ResponseStatus(HttpStatus.NOT_FOUND) public void handleTodoNotFound(TodoNotFoundException ex) { } }
If the validation fails, our REST API returns the validation errors as JSON and sets the response status to 400. If you want to know more about this, read a blog post titled: Spring from the Trenches: Adding Validation to a REST API.
That is it. We have now created a REST API that provides CRUD operations for todo entries and saves them to MongoDB database. Let’s summarize what we learned from this blog post.
Summary
This blog post has taught us three things:
- We can get the required dependencies with Maven by declaring only two dependencies: spring-boot-starter-web and spring-data-mongodb.
- If we are happy with the default configuration of Spring Boot, we can configure our web application by using its auto-configuration support and “dropping” new jars to the classpath.
- We learned to create a simple REST API that saves information to MongoDB database and finds information from it.
P.S. You can get the example application of this blog post from Github.
Reference: | Creating a REST API with Spring Boot and MongoDB from our JCG partner Petri Kainulainen at the Petri Kainulainen blog. |