FastFoodCoding

Login


Connect with one of the following accounts





Login

Please wait while we redirect...

Views: 18156   



I'll start this tutorial by keeping in mind that you already know the basics of Spring Boot.
If you are not familiar with Spring Boot I strongly recommend you to look at the following tutorial Which explains you the basics of Spring Boot. It explains you how to create your first spring boot application and how to add dependencies and how to make your first app up and running.
https://fastfoodcoding.com/tutorials/1503083112817/how-to-create-a-website-with-springboot-and-serve-static-content

Dependencies & Configurations

Let's jump into the matter, First thing you need to do is create the Spring Boot application and add the Spring Boot and Spring Data MongoDB dependencies to your build.gradle file

compile 'org.springframework.boot:spring-boot-starter-web:1.5.6.RELEASE'
compile 'org.springframework.data:spring-data-mongodb:1.10.6.RELEASE'

After adding these dependencies do "Refresh Gradle Project" so that Gradle downloads all the required dependencies for you.
Now add the database name to application.properties file, for this tutorial i am keeping my database name as example

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=example

Repository & Controller

Create your main program which starts the Spring Boot application

com.example.Start.java

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Start {
    public static void main(String[] args) {
        SpringApplication.run(Start.class, args);
    }
}

Create a User model pojo with the fields needed for User operations with setters & getters.

com.example.model.User

package com.example.model;

import org.springframework.data.annotation.Id;

public class User {
    @Id
    private String id;
    private String name;
    private int age;
    private String email;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

Now Create UserRepository Interface
com.example.repository.UserRepository

package com.example.repository;

import org.springframework.data.mongodb.repository.MongoRepository;

import com.example.model.User;

public interface UserRepository extends MongoRepository<User, String> {

    public User findOneByName(String name);

}

Now create the controller with Create, Read, Update & Delete REST endpoints
com.example.controller.UserController

package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.RestController;

import com.example.model.User;
import com.example.repository.UserRepository;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserRepository userRepository;

    @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public void create(@RequestBody User user) {
        userRepository.save(user);
    }

    @RequestMapping(value = "/{id}") 
    public User read(@PathVariable String id) {
        return userRepository.findOne(id);
    }

    @RequestMapping(method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
    public void update(@RequestBody User user) {
        userRepository.save(user);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) 
    public void delete(@PathVariable String id) {
        userRepository.delete(id); 
    }

}

Now you have all the Create, Read, Update & Delete operations enabled on the User collection. If you look at your project structure that should look like below

springboot-mongo-eclipse

Let's start testing them.
You may want to use a REST explorer to test these REST endpoints my favorite is Postman in this tutorial i'll show you the curl commands so that you can simply execute them from the command line.

Testing the REST endpoints

1. Create

http Request

POST /user HTTP/1.1
Host: localhost:8080
Content-Type: application/json

{
    "id" : "E01",
    "name" : "name 1",
    "age" : 20,
    "email" : "[email protected]"
}

CURL equivalent

curl -X POST \
  http://localhost:8080/user \
  -H 'content-type: application/json' \
  -d '{
    "id" : "E01",
    "name" : "name 1",
    "age" : 20,
    "email" : "[email protected]"
}'

If you run the above curl command from command prompt that will create the user document in example database.

2. Read

http Request

GET /user/E01 HTTP/1.1
Host: localhost:8080

CURL equivalent

curl -X GET \
  http://localhost:8080/user/E01 \

3. Update

http Request

PUT /user HTTP/1.1
Host: localhost:8080
Content-Type: application/json

{
    "id" : "E01",
    "_class" : "com.example.model.User",
    "name" : "name 1",
    "age" : 21,
    "email" : "[email protected]"
}

CURL equivalent

curl -X PUT \
  http://localhost:8080/user \
  -H 'content-type: application/json' \
  -d '{
    "id" : "E01",
    "_class" : "com.example.model.User",
    "name" : "name 1",
    "age" : 21,
    "email" : "[email protected]"
}'

4. Delete

http Request

DELETE /user/E01 HTTP/1.1
Host: localhost:8080

CURL equivalent

curl -X DELETE \
  http://localhost:8080/user/E01 \

You can find this sample project here https://github.com/fastfoodcoding/SpringBootMongoDbCRUD
You can download the sample project by hitting this link Download




On By


Top Tutorials

Top Questions

Top Articles

Top Blogs

Top News