{"id":34419,"date":"2014-12-16T01:00:04","date_gmt":"2014-12-15T23:00:04","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=34419"},"modified":"2014-12-15T16:44:23","modified_gmt":"2014-12-15T14:44:23","slug":"creating-a-rest-api-with-spring-boot-and-mongodb","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html","title":{"rendered":"Creating a REST API with Spring Boot and MongoDB"},"content":{"rendered":"<p><em>This year I greeted Christmas in a different fashion: I was a part of the <a href=\"http:\/\/www.javaadvent.com\/\" target=\"_blank\">Java Advent Calendar<\/a>. Let\u2019s boot up for Christmas:<\/em><\/p>\n<p><a href=\"http:\/\/projects.spring.io\/spring-boot\/\" target=\"_blank\">Spring Boot<\/a> 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\u2019t need an external servlet container.<\/p>\n<p>This sounds almost too good to be true, but <strong>Spring Boot can really do all this<\/strong>.<\/p>\n<p>This blog post demonstrates how easy it is to implement a REST API that provides CRUD operations for todo entries that are saved to <a href=\"http:\/\/www.mongodb.org\/\" target=\"_blank\">MongoDB<\/a> database.<\/p>\n<p>Let\u2019s start by creating our Maven project.<\/p>\n<p>This blog post assumes that you have already installed the MongoDB database. If you haven\u2019t done this, you can follow the instructions given in the blog post titled: <a href=\"http:\/\/spring.io\/guides\/gs\/accessing-data-mongodb\/#initial\" target=\"_blank\">Accessing Data with MongoDB<\/a>.<\/p>\n<h2>Creating Our Maven Project<\/h2>\n<p>We can create our Maven project by following these steps:<\/p>\n<ol>\n<li>Use the <em>spring-boot-starter-parent<\/em> POM as the parent POM of our Maven project. This ensures that our project inherits sensible default settings from Spring Boot.<\/li>\n<li>Add the <a href=\"http:\/\/docs.spring.io\/spring-boot\/docs\/1.1.9.RELEASE\/maven-plugin\/\" target=\"_blank\">Spring Boot Maven Plugin<\/a> 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.<\/li>\n<li>Configure the dependencies of our project. We need to configure the following dependencies:\n<ul>\n<li>The <em>spring-boot-starter-web<\/em> dependency provides the dependencies of a web application.<\/li>\n<li>The <em>spring-data-mongodb<\/em> dependency provides integration with the MongoDB document database.<\/li>\n<\/ul>\n<\/li>\n<li>Enable the Java 8 Support of Spring Boot.<\/li>\n<li>Configure the main class of our application. This class is responsible of configuring and starting our application.<\/li>\n<\/ol>\n<p>The relevant part of our <em>pom.xml<\/em> file looks as follows:<\/p>\n<pre class=\" brush:xml\">&lt;properties&gt;\r\n    &lt;!-- Enable Java 8 --&gt;\r\n    &lt;java.version&gt;1.8&lt;\/java.version&gt;\r\n    &lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt;\r\n    &lt;!-- Configure the main class of our Spring Boot application --&gt;\r\n    &lt;start-class&gt;com.javaadvent.bootrest.TodoAppConfig&lt;\/start-class&gt;\r\n&lt;\/properties&gt;\r\n        \r\n&lt;!-- Inherit defaults from Spring Boot --&gt;\r\n&lt;parent&gt;\r\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;\r\n    &lt;version&gt;1.1.9.RELEASE&lt;\/version&gt;\r\n&lt;\/parent&gt;\r\n\r\n&lt;dependencies&gt;\r\n    &lt;!-- Get the dependencies of a web application --&gt;\r\n    &lt;dependency&gt;\r\n        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n        &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\r\n    &lt;\/dependency&gt;\r\n\r\n    &lt;!-- Spring Data MongoDB--&gt;\r\n    &lt;dependency&gt;\r\n        &lt;groupId&gt;org.springframework.data&lt;\/groupId&gt;\r\n        &lt;artifactId&gt;spring-data-mongodb&lt;\/artifactId&gt;\r\n    &lt;\/dependency&gt;\r\n&lt;\/dependencies&gt;\r\n\r\n&lt;build&gt;\r\n    &lt;plugins&gt;\r\n        &lt;!-- Spring Boot Maven Support --&gt;\r\n        &lt;plugin&gt;\r\n            &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;\r\n        &lt;\/plugin&gt;\r\n    &lt;\/plugins&gt;\r\n&lt;\/build&gt;<\/pre>\n<h4>Additional Reading:<\/h4>\n<ul>\n<li><a href=\"http:\/\/docs.spring.io\/spring-boot\/docs\/1.1.9.RELEASE\/reference\/htmlsingle\/#getting-started-maven-installation\" target=\"_blank\">Spring Boot Reference Manual: 9.1.1 Maven installation<\/a><\/li>\n<li><a href=\"http:\/\/docs.spring.io\/spring-boot\/docs\/1.1.9.RELEASE\/reference\/htmlsingle\/#using-boot-maven\" target=\"_blank\">Spring Boot Reference Manual: 12.1 Maven<\/a><\/li>\n<li><a href=\"http:\/\/docs.spring.io\/spring-boot\/docs\/1.1.9.RELEASE\/maven-plugin\/usage.html\" target=\"_blank\">Spring Boot Maven Plugin \u2013 Usage<\/a><\/li>\n<\/ul>\n<p>Let\u2019s move on and find out how we can configure our application.<\/p>\n<h2>Configuring Our Application<\/h2>\n<p>We can configure our Spring Boot application by following these steps:<\/p>\n<ol>\n<li>Create a <em>TodoAppConfig<\/em> class to the <em>com.javaadvent.bootrest<\/em> package.<\/li>\n<li>Enable Spring Boot auto-configuration.<\/li>\n<li>Configure the Spring container to scan components found from the child packages of the <em>com.javaadvent.bootrest<\/em> package.<\/li>\n<li>Add the <em>main()<\/em> method to the <em>TodoAppConfig<\/em> class and implement by running our application.<\/li>\n<\/ol>\n<p>The source code of the <em>TodoAppConfig<\/em> class looks as follows:<\/p>\n<pre class=\" brush:java\">package com.javaadvent.bootrest;\r\n\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.boot.autoconfigure.EnableAutoConfiguration;\r\nimport org.springframework.context.annotation.ComponentScan;\r\nimport org.springframework.context.annotation.Configuration;\r\n\r\n@Configuration\r\n@EnableAutoConfiguration\r\n@ComponentScan\r\npublic class TodoAppConfig {\r\n    \r\n    public static void main(String[] args) {\r\n        SpringApplication.run(TodoAppConfig.class, args);\r\n    }\r\n}<\/pre>\n<p>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.<\/p>\n<h4>Additional Reading:<\/h4>\n<ul>\n<li><a href=\"http:\/\/docs.spring.io\/spring-boot\/docs\/1.1.9.RELEASE\/reference\/htmlsingle\/#using-boot-locating-the-main-class\" target=\"_blank\">Spring Boot Reference Manual: 13.2 Location the main application class<\/a><\/li>\n<li><a href=\"http:\/\/docs.spring.io\/spring-boot\/docs\/1.1.9.RELEASE\/reference\/htmlsingle\/#using-boot-configuration-classes\" target=\"_blank\">Spring Boot Reference Manual: 14. Configuration classes<\/a><\/li>\n<li><a href=\"http:\/\/docs.spring.io\/spring-boot\/docs\/1.1.9.RELEASE\/api\/org\/springframework\/boot\/autoconfigure\/EnableAutoConfiguration.html\" target=\"_blank\"> The Javadoc of the <em>@EnableAutoConfiguration<\/em> annotation<\/a><\/li>\n<li><a href=\"http:\/\/docs.spring.io\/spring-boot\/docs\/1.1.9.RELEASE\/reference\/htmlsingle\/#using-boot-auto-configuration\" target=\"_blank\">Spring Boot Reference Manual: 15. Auto-configuration<\/a><\/li>\n<li><a href=\"http:\/\/docs.spring.io\/spring-boot\/docs\/1.1.9.RELEASE\/api\/org\/springframework\/boot\/SpringApplication.html\" target=\"_blank\">The Javadoc of the <em>SpringApplication<\/em> class<\/a><\/li>\n<li><a href=\"http:\/\/docs.spring.io\/spring-boot\/docs\/current\/reference\/htmlsingle\/#boot-features-connecting-to-mongodb\" target=\"_blank\">Spring Boot Reference Manual: 27.2.1 Connecting to a MongoDB database<\/a><\/li>\n<\/ul>\n<p>Let\u2019s move on and implement our REST API.<\/p>\n<h2>Implementing Our REST API<\/h2>\n<p>We need implement a REST API that provides CRUD operations for todo entries. The requirements of our REST API are:<\/p>\n<ul>\n<li>A <em>POST<\/em> request send to the url \u2018\/api\/todo\u2019 must create a new todo entry by using the information found from the request body and return the information of the created todo entry.<\/li>\n<li>A <em>DELETE<\/em> request send to the url \u2018\/api\/todo\/{id}\u2019 must delete the todo entry whose id is found from the url and return the information of the deleted todo entry.<\/li>\n<li>A <em>GET<\/em> request send to the url \u2018\/api\/todo\u2019 must return all todo entries that are found from the database.<\/li>\n<li>A <em>GET<\/em> request send to the url \u2018\/api\/todo\/{id}\u2019 must return the information of the todo entry whose id is found from the url.<\/li>\n<li>A <em>PUT<\/em> request send to the url \u2018\/api\/todo\/{id}\u2019 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.<\/li>\n<\/ul>\n<p>We can fulfill these requirements by following these steps:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<ol>\n<li>Create the entity that contains the information of a single todo entry.<\/li>\n<li>Create the repository that is used to save todo entries to MongoDB database and find todo entries from it.<\/li>\n<li>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.<\/li>\n<li>Create the controller class that processes HTTP requests and returns the correct response back to the client.<\/li>\n<\/ol>\n<p>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.<\/p>\n<p>Let\u2019s get started.<\/p>\n<h3>Creating the Entity<\/h3>\n<p>We need to create the entity class that contains the information of a single todo entry. We can do this by following these steps:<\/p>\n<ol>\n<li>Add the <em>id<\/em>, <em>description<\/em>, and <em>title<\/em> fields to the created entity class. Configure the id field of the entity by annotating the <em>id<\/em> field with the <em>@Id<\/em> annotation.<\/li>\n<li>Specify the constants (<em>MAX_LENGTH_DESCRIPTION<\/em> and <em>MAX_LENGTH_TITLE<\/em>) that specify the maximum length of the <em>description<\/em> and <em>title<\/em> fields.<\/li>\n<li>Add a static builder class to the entity class. This class is used to create new <em>Todo<\/em> objects.<\/li>\n<li>Add an <em>update()<\/em> method to the entity class. This method simply updates the <em>title<\/em> and <em>description<\/em> of the entity if valid values are given as method parameters.<\/li>\n<\/ol>\n<p>The source code of the <em>Todo<\/em> class looks as follows:<\/p>\n<pre class=\" brush:java\">import org.springframework.data.annotation.Id;\r\n\r\nimport static com.javaadvent.bootrest.util.PreCondition.isTrue;\r\nimport static com.javaadvent.bootrest.util.PreCondition.notEmpty;\r\nimport static com.javaadvent.bootrest.util.PreCondition.notNull;\r\n\r\nfinal class Todo {\r\n\r\n    static final int MAX_LENGTH_DESCRIPTION = 500;\r\n    static final int MAX_LENGTH_TITLE = 100;\r\n\r\n    @Id\r\n    private String id;\r\n\r\n    private String description;\r\n\r\n    private String title;\r\n\r\n    public Todo() {}\r\n\r\n    private Todo(Builder builder) {\r\n        this.description = builder.description;\r\n        this.title = builder.title;\r\n    }\r\n\r\n    static Builder getBuilder() {\r\n        return new Builder();\r\n    }\r\n\r\n    \/\/Other getters are omitted\r\n\r\n    public void update(String title, String description) {\r\n        checkTitleAndDescription(title, description);\r\n\r\n        this.title = title;\r\n        this.description = description;\r\n    }\r\n\r\n    \/**\r\n     * We don't have to use the builder pattern here because the constructed \r\n     * class has only two String fields. However, I use the builder pattern \r\n     * in this example because it makes the code a bit easier to read.\r\n     *\/\r\n    static class Builder {\r\n\r\n        private String description;\r\n\r\n        private String title;\r\n\r\n        private Builder() {}\r\n\r\n        Builder description(String description) {\r\n            this.description = description;\r\n            return this;\r\n        }\r\n\r\n        Builder title(String title) {\r\n            this.title = title;\r\n            return this;\r\n        }\r\n\r\n        Todo build() {\r\n            Todo build = new Todo(this);\r\n\r\n            build.checkTitleAndDescription(build.getTitle(), build.getDescription());\r\n\r\n            return build;\r\n        }\r\n    }\r\n\r\n    private void checkTitleAndDescription(String title, String description) {\r\n        notNull(title, \"Title cannot be null\");\r\n        notEmpty(title, \"Title cannot be empty\");\r\n        isTrue(title.length() &lt;= MAX_LENGTH_TITLE,\r\n                \"Title cannot be longer than %d characters\",\r\n                MAX_LENGTH_TITLE\r\n        );\r\n\r\n        if (description != null) {\r\n            isTrue(description.length() &lt;= MAX_LENGTH_DESCRIPTION,\r\n                    \"Description cannot be longer than %d characters\",\r\n                    MAX_LENGTH_DESCRIPTION\r\n            );\r\n        }\r\n    }\r\n}<\/pre>\n<h4>Additional Reading:<\/h4>\n<ul>\n<li><a href=\"http:\/\/www.informit.com\/articles\/article.aspx?p=1216151&amp;seqNum=2\" target=\"_blank\">Item 2: Consider a builder when faced with many constructor parameters<\/a><\/li>\n<\/ul>\n<p>Let\u2019s move on and create the repository that communicates with the MongoDB database.<\/p>\n<h3>Creating the Repository<\/h3>\n<p>We have to create the repository interface that is used to save <em>Todo<\/em> objects to MondoDB database and retrieve <em>Todo<\/em> objects from it.<\/p>\n<p>If we don\u2019t want to use the Java 8 support of Spring Data, we could create our repository by creating an interface that extends the <em>CrudRepository&lt;T, ID&gt;<\/em> interface. However, because we want to use the Java 8 support, we have to follow these steps:<\/p>\n<ol>\n<li>Create an interface that extends the <em>Repository&lt;T, ID&gt;<\/em> interface.<\/li>\n<li>Add the following repository methods to the created interface:\n<ol>\n<li>The <em>void delete(Todo deleted)<\/em> method deletes the todo entry that is given as a method parameter.<\/li>\n<li>The <em><em>List<\/em><\/em>findAll()method returns all todo entries that are found from the database.<\/li>\n<li>The <em><em>Optional<\/em><\/em>findOne(String id)method returns the information of a single todo entry. If no todo entry is found, this method returns an empty <em>Optional<\/em>.<\/li>\n<li>The <em>Todo save(Todo saved)<\/em> method saves a new todo entry to the database and returns the the saved todo entry.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>The source code of the <em>TodoRepository<\/em> interface looks as follows:<\/p>\n<pre class=\" brush:java\">import org.springframework.data.repository.Repository;\r\n\r\nimport java.util.List;\r\nimport java.util.Optional;\r\n\r\ninterface TodoRepository extends Repository&lt;Todo, String&gt; {\r\n\r\n    void delete(Todo deleted);\r\n\r\n    List&lt;Todo&gt; findAll();\r\n\r\n    Optional&lt;Todo&gt; findOne(String id);\r\n\r\n    Todo save(Todo saved);\r\n}<\/pre>\n<h4>Additional Reading:<\/h4>\n<ul>\n<li><a href=\"http:\/\/docs.spring.io\/spring-data\/commons\/docs\/current\/api\/org\/springframework\/data\/repository\/CrudRepository.html\" target=\"_blank\">The Javadoc of the <em>CrudRepository&lt;T, ID&gt;<\/em> interface<\/a><\/li>\n<li><a href=\"http:\/\/docs.spring.io\/spring-data\/commons\/docs\/current\/api\/org\/springframework\/data\/repository\/Repository.html\" target=\"_blank\">The Javadoc of the <em>Repository&lt;T, ID&gt;<\/em> interface<\/a><\/li>\n<li><a href=\"http:\/\/docs.spring.io\/spring-data\/mongodb\/docs\/current\/reference\/html\/#repositories\" target=\"_blank\">Spring Data MongoDB Reference Manual: 5. Working with Spring Data Repositories<\/a><\/li>\n<li><a href=\"http:\/\/docs.spring.io\/spring-data\/mongodb\/docs\/current\/reference\/html\/#repositories.definition-tuning\" target=\"_blank\">Spring Data MongoDB Reference Manual: 5.3.1 Fine-tuning repository definition<\/a><\/li>\n<\/ul>\n<p>Let\u2019s move on and create the service layer of our example application.<\/p>\n<h3>Creating the Service Layer<\/h3>\n<p><strong>First<\/strong>, we have to create a service interface that provides CRUD operations for todo entries. The source code of the <em>TodoService<\/em> interface looks as follows:<\/p>\n<pre class=\" brush:java\">import java.util.List;\r\n\r\ninterface TodoService {\r\n\r\n    TodoDTO create(TodoDTO todo);\r\n\r\n    TodoDTO delete(String id);\r\n\r\n    List&lt;TodoDTO&gt; findAll();\r\n\r\n    TodoDTO findById(String id);\r\n\r\n    TodoDTO update(TodoDTO todo);\r\n}<\/pre>\n<p>The <em>TodoDTO<\/em> 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.<\/p>\n<p><strong>Second<\/strong>, we have to implement the TodoService interface. We can do this by following these steps:<\/p>\n<ol>\n<li>Inject our repository to the service class by using constructor injection.<\/li>\n<li>Add a <em>private Todo findTodoById(String id)<\/em> method to the service class and implement it by either returning the found <em>Todo<\/em> object or throwing the <em>TodoNotFoundException<\/em>.<\/li>\n<li>Add a <em>private TodoDTO convertToDTO(Todo model)<\/em> method the service class and implement it by converting the <em>Todo<\/em> object into a <em>TodoDTO<\/em> object and returning the created object.<\/li>\n<li>Add a <em><em>private List<\/em><\/em>convertToDTOs(Listmodels)and implement it by converting the list of <em>Todo<\/em> objects into a list of <em>TodoDTO<\/em> objects and returning the created list.<\/li>\n<li>Implement the <em>TodoDTO create(TodoDTO todo)<\/em> method. This method creates a new <em>Todo<\/em> object, saves the created object to the MongoDB database, and returns the information of the created todo entry.<\/li>\n<li>Implement the <em>TodoDTO delete(String id)<\/em> method. This method finds the deleted <em>Todo<\/em> object, deletes it, and returns the information of the deleted todo entry. If no <em>Todo<\/em> object is found with the given id, this method throws the <em>TodoNotFoundException<\/em>.<\/li>\n<li>Implement the <em><em>List<\/em><\/em>findAll()method. This methods retrieves all <em>Todo<\/em> objects from the database, transforms them into a list of <em>TodoDTO<\/em> objects, and returns the created list.<\/li>\n<li>Implement the <em>TodoDTO findById(String id)<\/em> method. This method finds the <em>Todo<\/em> object from the database, converts it into a <em>TodoDTO<\/em> object, and returns the created <em>TodoDTO<\/em> object. If no todo entry is found, this method throws the <em>TodoNotFoundException<\/em>.<\/li>\n<li>Implement the <em>TodoDTO update(TodoDTO todo)<\/em> method. This method finds the updated <em>Todo<\/em> object from the database, updates its <em>title<\/em> and <em>description<\/em>, saves it, and returns the updated information. If the updated <em>Todo<\/em> object is not found, this method throws the <em>TodoNotFoundException<\/em>.<\/li>\n<\/ol>\n<p>The source code of the <em>MongoDBTodoService<\/em> looks as follows:<\/p>\n<pre class=\" brush:java\">import org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.stereotype.Service;\r\n\r\nimport java.util.List;\r\nimport java.util.Optional;\r\n\r\nimport static java.util.stream.Collectors.toList;\r\n\r\n@Service\r\nfinal class MongoDBTodoService implements TodoService {\r\n\r\n    private final TodoRepository repository;\r\n\r\n    @Autowired\r\n    MongoDBTodoService(TodoRepository repository) {\r\n        this.repository = repository;\r\n    }\r\n\r\n    @Override\r\n    public TodoDTO create(TodoDTO todo) {\r\n        Todo persisted = Todo.getBuilder()\r\n                .title(todo.getTitle())\r\n                .description(todo.getDescription())\r\n                .build();\r\n        persisted = repository.save(persisted);\r\n        return convertToDTO(persisted);\r\n    }\r\n\r\n    @Override\r\n    public TodoDTO delete(String id) {\r\n        Todo deleted = findTodoById(id);\r\n        repository.delete(deleted);\r\n        return convertToDTO(deleted);\r\n    }\r\n\r\n    @Override\r\n    public List&lt;tododto&gt; findAll() {\r\n        List&lt;todo&gt; todoEntries = repository.findAll();\r\n        return convertToDTOs(todoEntries);\r\n    }\r\n\r\n    private List&lt;tododto&gt; convertToDTOs(List&lt;todo&gt; models) {\r\n        return models.stream()\r\n                .map(this::convertToDTO)\r\n                .collect(toList());\r\n    }\r\n\r\n    @Override\r\n    public TodoDTO findById(String id) {\r\n        Todo found = findTodoById(id);\r\n        return convertToDTO(found);\r\n    }\r\n\r\n    @Override\r\n    public TodoDTO update(TodoDTO todo) {\r\n        Todo updated = findTodoById(todo.getId());\r\n        updated.update(todo.getTitle(), todo.getDescription());\r\n        updated = repository.save(updated);\r\n        return convertToDTO(updated);\r\n    }\r\n\r\n    private Todo findTodoById(String id) {\r\n        Optional&lt;todo&gt; result = repository.findOne(id);\r\n        return result.orElseThrow(() -&gt; new TodoNotFoundException(id));\r\n\r\n    }\r\n\r\n    private TodoDTO convertToDTO(Todo model) {\r\n        TodoDTO dto = new TodoDTO();\r\n\r\n        dto.setId(model.getId());\r\n        dto.setTitle(model.getTitle());\r\n        dto.setDescription(model.getDescription());\r\n\r\n        return dto;\r\n    }\r\n}<\/pre>\n<p>We have now created the service layer of our example application. Let\u2019s move on and create the controller class.<\/p>\n<h3>Creating the Controller Class<\/h3>\n<p><strong>First<\/strong>, 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 <em>TodoDTO<\/em> class looks as follows:<\/p>\n<pre class=\" brush:java\">import org.hibernate.validator.constraints.NotEmpty;\r\n\r\nimport javax.validation.constraints.Size;\r\n\r\npublic final class TodoDTO {\r\n\r\n    private String id;\r\n\r\n    @Size(max = Todo.MAX_LENGTH_DESCRIPTION)\r\n    private String description;\r\n\r\n    @NotEmpty\r\n    @Size(max = Todo.MAX_LENGTH_TITLE)\r\n    private String title;\r\n\r\n    \/\/Constructor, getters, and setters are omitted\r\n}<\/pre>\n<h4>Additional Reading:<\/h4>\n<ul>\n<li><a href=\"http:\/\/docs.jboss.org\/hibernate\/validator\/5.0\/reference\/en-US\/html_single\/\" target=\"_blank\">The Reference Manual of Hibernate Validator 5.0.3<\/a><\/li>\n<\/ul>\n<p><strong>Second<\/strong>, 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:<\/p>\n<ol>\n<li>Inject our service to our controller by using constructor injection.<\/li>\n<li>Add a <em>create()<\/em> method to our controller and implement it by following these steps:\n<ol>\n<li>Read the information of the created todo entry from the request body.<\/li>\n<li>Validate the information of the created todo entry.<\/li>\n<li>Create a new todo entry and return the created todo entry. Set the response status to 201.<\/li>\n<\/ol>\n<\/li>\n<li>Implement the <em>delete()<\/em> method by delegating the id of the deleted todo entry forward to our service and return the deleted todo entry.<\/li>\n<li>Implement the <em>findAll()<\/em> method by finding the todo entries from the database and returning the found todo entries.<\/li>\n<li>Implement the <em>findById()<\/em> method by finding the todo entry from the database and returning the found todo entry.<\/li>\n<li>Implement the <em>update()<\/em> method by following these steps:\n<ol>\n<li>Read the information of the updated todo entry from the request body.<\/li>\n<li>Validate the information of the updated todo entry.<\/li>\n<li>Update the information of the todo entry and return the updated todo entry.<\/li>\n<\/ol>\n<\/li>\n<li>Create an <em>@ExceptionHandler<\/em> method that sets the response status to 404 if the todo entry was not found (<em>TodoNotFoundException<\/em> was thrown).<\/li>\n<\/ol>\n<p>The source code of the <em>TodoController<\/em> class looks as follows:<\/p>\n<pre class=\" brush:java\">import org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.http.HttpStatus;\r\nimport org.springframework.web.bind.annotation.ExceptionHandler;\r\nimport org.springframework.web.bind.annotation.PathVariable;\r\nimport org.springframework.web.bind.annotation.RequestBody;\r\nimport org.springframework.web.bind.annotation.RequestMapping;\r\nimport org.springframework.web.bind.annotation.RequestMethod;\r\nimport org.springframework.web.bind.annotation.ResponseStatus;\r\nimport org.springframework.web.bind.annotation.RestController;\r\n\r\nimport javax.validation.Valid;\r\nimport java.util.List;\r\n\r\n@RestController\r\n@RequestMapping(\"\/api\/todo\")\r\nfinal class TodoController {\r\n\r\n    private final TodoService service;\r\n\r\n    @Autowired\r\n    TodoController(TodoService service) {\r\n        this.service = service;\r\n    }\r\n\r\n    @RequestMapping(method = RequestMethod.POST)\r\n    @ResponseStatus(HttpStatus.CREATED)\r\n    TodoDTO create(@RequestBody @Valid TodoDTO todoEntry) {\r\n        return service.create(todoEntry);\r\n    }\r\n\r\n    @RequestMapping(value = \"{id}\", method = RequestMethod.DELETE)\r\n    TodoDTO delete(@PathVariable(\"id\") String id) {\r\n        return service.delete(id);\r\n    }\r\n\r\n    @RequestMapping(method = RequestMethod.GET)\r\n    List&lt;TodoDTO&gt; findAll() {\r\n        return service.findAll();\r\n    }\r\n\r\n    @RequestMapping(value = \"{id}\", method = RequestMethod.GET)\r\n    TodoDTO findById(@PathVariable(\"id\") String id) {\r\n        return service.findById(id);\r\n    }\r\n\r\n    @RequestMapping(value = \"{id}\", method = RequestMethod.PUT)\r\n    TodoDTO update(@RequestBody @Valid TodoDTO todoEntry) {\r\n        return service.update(todoEntry);\r\n    }\r\n\r\n    @ExceptionHandler\r\n    @ResponseStatus(HttpStatus.NOT_FOUND)\r\n    public void handleTodoNotFound(TodoNotFoundException ex) {\r\n    }\r\n}<\/pre>\n<p>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: <a href=\"http:\/\/www.javacodegeeks.com\/2013\/05\/spring-from-the-trenches-adding-validation-to-a-rest-api.html\">Spring from the Trenches: Adding Validation to a REST API<\/a>.<\/p>\n<p>That is it. We have now created a REST API that provides CRUD operations for todo entries and saves them to MongoDB database. Let\u2019s summarize what we learned from this blog post.<\/p>\n<h2>Summary<\/h2>\n<p>This blog post has taught us three things:<\/p>\n<ul>\n<li>We can get the required dependencies with Maven by declaring only two dependencies: <em>spring-boot-starter-web<\/em> and <em>spring-data-mongodb<\/em>.<\/li>\n<li>If we are happy with the default configuration of Spring Boot, we can configure our web application by using its auto-configuration support and \u201cdropping\u201d new jars to the classpath.<\/li>\n<li>We learned to create a simple REST API that saves information to MongoDB database and finds information from it.<\/li>\n<\/ul>\n<p><strong>P.S.<\/strong> You can <a href=\"https:\/\/github.com\/pkainulainen\/java-advent-2014\" target=\"_blank\">get the example application of this blog post from Github<\/a>.<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"http:\/\/www.petrikainulainen.net\/programming\/spring-framework\/creating-a-rest-api-with-spring-boot-and-mongodb\/\">Creating a REST API with Spring Boot and MongoDB<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/jcg\/\">JCG partner<\/a> Petri Kainulainen at the <a href=\"http:\/\/www.petrikainulainen.net\/\">Petri Kainulainen<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This year I greeted Christmas in a different fashion: I was a part of the Java Advent Calendar. Let\u2019s 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 &hellip;<\/p>\n","protected":false},"author":429,"featured_media":240,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[30,854],"class_list":["post-34419","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-spring","tag-spring-boot"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Creating a REST API with Spring Boot and MongoDB<\/title>\n<meta name=\"description\" content=\"This year I greeted Christmas in a different fashion: I was a part of the Java Advent Calendar. Let\u2019s boot up for Christmas: Spring Boot is an opinionated\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating a REST API with Spring Boot and MongoDB\" \/>\n<meta property=\"og:description\" content=\"This year I greeted Christmas in a different fashion: I was a part of the Java Advent Calendar. Let\u2019s boot up for Christmas: Spring Boot is an opinionated\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2014-12-15T23:00:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Petri Kainulainen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/petrikainulaine\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Petri Kainulainen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/creating-a-rest-api-with-spring-boot-and-mongodb.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/creating-a-rest-api-with-spring-boot-and-mongodb.html\"},\"author\":{\"name\":\"Petri Kainulainen\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/5af4df3fdfeb79e9fa3598d79bff2c9e\"},\"headline\":\"Creating a REST API with Spring Boot and MongoDB\",\"datePublished\":\"2014-12-15T23:00:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/creating-a-rest-api-with-spring-boot-and-mongodb.html\"},\"wordCount\":2019,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/creating-a-rest-api-with-spring-boot-and-mongodb.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"keywords\":[\"Spring\",\"Spring Boot\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/creating-a-rest-api-with-spring-boot-and-mongodb.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/creating-a-rest-api-with-spring-boot-and-mongodb.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/creating-a-rest-api-with-spring-boot-and-mongodb.html\",\"name\":\"Creating a REST API with Spring Boot and MongoDB\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/creating-a-rest-api-with-spring-boot-and-mongodb.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/creating-a-rest-api-with-spring-boot-and-mongodb.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"datePublished\":\"2014-12-15T23:00:04+00:00\",\"description\":\"This year I greeted Christmas in a different fashion: I was a part of the Java Advent Calendar. Let\u2019s boot up for Christmas: Spring Boot is an opinionated\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/creating-a-rest-api-with-spring-boot-and-mongodb.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/creating-a-rest-api-with-spring-boot-and-mongodb.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/creating-a-rest-api-with-spring-boot-and-mongodb.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"spring-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/creating-a-rest-api-with-spring-boot-and-mongodb.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Creating a REST API with Spring Boot and MongoDB\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/5af4df3fdfeb79e9fa3598d79bff2c9e\",\"name\":\"Petri Kainulainen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9e57425180f323fa65bc519a64c8273d3fcb7c6bd272e56b37dd15613f403659?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9e57425180f323fa65bc519a64c8273d3fcb7c6bd272e56b37dd15613f403659?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9e57425180f323fa65bc519a64c8273d3fcb7c6bd272e56b37dd15613f403659?s=96&d=mm&r=g\",\"caption\":\"Petri Kainulainen\"},\"description\":\"Petri is passionate about software development and continuous improvement. He is specialized in software development with the Spring Framework and is the author of Spring Data book.\",\"sameAs\":[\"http:\\\/\\\/www.petrikainulainen.net\\\/\",\"http:\\\/\\\/www.linkedin.com\\\/in\\\/petrikainulainen\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/petrikainulaine\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/petri-kainulainen\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating a REST API with Spring Boot and MongoDB","description":"This year I greeted Christmas in a different fashion: I was a part of the Java Advent Calendar. Let\u2019s boot up for Christmas: Spring Boot is an opinionated","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html","og_locale":"en_US","og_type":"article","og_title":"Creating a REST API with Spring Boot and MongoDB","og_description":"This year I greeted Christmas in a different fashion: I was a part of the Java Advent Calendar. Let\u2019s boot up for Christmas: Spring Boot is an opinionated","og_url":"https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2014-12-15T23:00:04+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","type":"image\/jpeg"}],"author":"Petri Kainulainen","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/petrikainulaine","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Petri Kainulainen","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html"},"author":{"name":"Petri Kainulainen","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/5af4df3fdfeb79e9fa3598d79bff2c9e"},"headline":"Creating a REST API with Spring Boot and MongoDB","datePublished":"2014-12-15T23:00:04+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html"},"wordCount":2019,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","keywords":["Spring","Spring Boot"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html","url":"https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html","name":"Creating a REST API with Spring Boot and MongoDB","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","datePublished":"2014-12-15T23:00:04+00:00","description":"This year I greeted Christmas in a different fashion: I was a part of the Java Advent Calendar. Let\u2019s boot up for Christmas: Spring Boot is an opinionated","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","width":150,"height":150,"caption":"spring-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/creating-a-rest-api-with-spring-boot-and-mongodb.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Creating a REST API with Spring Boot and MongoDB"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/5af4df3fdfeb79e9fa3598d79bff2c9e","name":"Petri Kainulainen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/9e57425180f323fa65bc519a64c8273d3fcb7c6bd272e56b37dd15613f403659?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/9e57425180f323fa65bc519a64c8273d3fcb7c6bd272e56b37dd15613f403659?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9e57425180f323fa65bc519a64c8273d3fcb7c6bd272e56b37dd15613f403659?s=96&d=mm&r=g","caption":"Petri Kainulainen"},"description":"Petri is passionate about software development and continuous improvement. He is specialized in software development with the Spring Framework and is the author of Spring Data book.","sameAs":["http:\/\/www.petrikainulainen.net\/","http:\/\/www.linkedin.com\/in\/petrikainulainen","https:\/\/x.com\/https:\/\/twitter.com\/petrikainulaine"],"url":"https:\/\/www.javacodegeeks.com\/author\/petri-kainulainen"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/34419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/429"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=34419"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/34419\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/240"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=34419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=34419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=34419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}