Spring Boot PUT Request with MongoDB

Nuwan Harshakumara Piyarathna
3 min readJul 15, 2021

--

This tutorial is the continuation of the previous tutorial where we learned how to create a Spring Boot Post Request with MongoDB .(link to previous tutorial ).

Now we have created a Student Model class to represent a Student Object. In the Controller class, we have created a method to Post Students to the database. Currently, we have few Students in our MongoDB database and What we want is to create a method to update selected Student’s data. Let’s start.

Create a UpdatePayLoad

Select the package and create a new Java class named “StudentUpdatePayLoad” . This class is almost the same as the Student class. But the difference is we only define the attributes of the Student Object that we want to change. (Note that we do not want to change the attributes like Id of the Student ).

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class StudentUpdatePayLoad {
String name;
int age;
}

Let’s assume we want to update the name and age of a particular Student. Then our StudentUpdatePayLoad class can be created like above.

Create a PUT Request

Open the StudentController class and Let’s create a PUT request to update a Student.

@PutMapping(value = "/update/{id}")
public ResponseEntity<?> updateUser(@PathVariable("id")String id, @RequestBody StudentUpdatePayLoad studentUpdatePayLoad){
Optional<Student> student = studentRepository.findById(id);
if(!student.isPresent()){
return new ResponseEntity<>(
"student not found",
HttpStatus.BAD_REQUEST
);
}
student.ifPresent(s -> {
s.setName(studentUpdatePayLoad.getName());
s.setAge(studentUpdatePayLoad.getAge());
studentRepository.save(s);
});
return new ResponseEntity<>(
"Student Successfully Updated",
HttpStatus.OK
);
}

Here we want must send the id of the Student, that we want to update as a Path Variable. Updated data must be sent as described in StudentUpdatePayLoad class. Note that here, first, we have to check whether the Student with the corresponding id is in the database. If not we must send the Corresponding error message. If the student is present in the database, then we can update the data. And Finally, we send a Success message to say that Student Update was Successful.

Now we are done with our program with Spring Boot. Easy right?

Next, simply click the Run button on the top section of the IntelliJ IDEA IDE to run the Spring Boot Project.

Test

Open MongoDB Compass and Go to School database. Select Our Student Collection and In my case, It looks like this. If your Collection has no documents,then please refer to the previous tutorial to add some Students to the Collection.

Now I am going to change the Student details with the id “60bf0bb9953cd9731a30521a”.Open Postman to create a PUT request. Select the Request type as PUT and enter the request URL. Here I have sent StudentUpdatePayLoad as a JSON object. In the URL we must give the id of the Student that we want to update as a path variable.

Click Send to send the request to the server. You will get a response with a status code 200. It will say “Student Successfully Updated” message.

Now simply refresh the Student Collection in MongoDB Compass to see whether it actually worked.

As you can see, the Student name and age have changed because of our PUT Request.

Congratulations ! you have successfully created a Spring Boot PUT request using a MongoDB database.

--

--

Nuwan Harshakumara Piyarathna
Nuwan Harshakumara Piyarathna

Written by Nuwan Harshakumara Piyarathna

Software Engineer Intern at WSO2 | Computer Engineering Undergraduate of University of Peradeniya | https://nuwan.me

No responses yet