January 13, 2021

Spring Boot – Write POST Methods

In this lesson we would write the post methods. This method would lets use insert new records. For example, create new user, new location and new post.


  1. Create New Location
  2. Install Advanced REST Client
  3. Test the POST Method
  4. Create new User
  5. Create new Post


1. Create New Location

We start by creating a new location. To insert a new resource, the HTTP method will be POST. This would be added as part of the parameters to the RequestMapping annotation as show below.


@RequestMapping(method=RequestMethod.POST, value="/locations")
    public void addLocation() {


You can see that the url for inserting a new location is same as the url for getAllLocations. We used this when we created the GET methods.

The complete method is given below:


@RequestMapping(method=RequestMethod.POST, value="/locations")
public void addLocation(@RequestBody Location location) {


Now, the addLocation() method have not been implemented in the LocationService. So you need to create this method in the LocationService class.

Put the code below in the LocationService


public void addLocation(Location location) {


Next, we are going to test the post methods.To do this we would need a tool.



2. Install Advanced REST Client

To make a POST request, we need to provide the data we want to insert. Since this is an API, we don’t have a HTML form to fill in this data.

So to provide input, we need to use a tool called Advanced REST Client. This is a free plugin to Google Chrome.

To install it, visit this link (Get Advanced REST Client) and just install it. Next, launch it. The interface is as shown below. You can watch the video to see the procedure.

Watch the video of How to Use Advanced REST Client



3. Test the POST Method

Before you do the test, you need to make a little change to the locations variable in the LocationService class.

Replace this existing location variables,

List<Location> locations = Arrays.asList(location1, location2, location3);


with this one;

List<Location> locations = new ArrayList<>(Arrays.asList(location1, location2, location3));


This would make it possible to modify the ArrayList

Now, test the POST Method using Advanced REST Client.



4. Create New User

Write the addUser method in the UserController class.

In the controller file, replace the line:

private List<User> users = Arrays.asList(user1, user2);	


with this one


private List<User> users = new ArrayList<>(Arrays.asList(user1, user2));


The addUser method for the UserController class is given below. You can copy and paste it.


@RequestMapping(value="/users", method = RequestMethod.POST)
public void addUser(@RequestBody User user) {


Finally, place the addUser method for the UserService class


public void addUser(User user) {


You can then test the application using Advanced REST Client. Try to add a new User.



5. Create New Post

Finally, we would work on inserting a new Post. As before, replace this line in PostService

List<Post> posts = Arrays.asList(post1, post2);


with this one:

List<Post> posts = new ArrayList<>(Arrays.asList(post1, post2));


The addPost method for the PostController class is as shown below


@RequestMapping(value = "/posts", method = RequestMethod.POST)
public void addPost(@RequestBody Post post) {


The addPost method for the PostService class is as below:

public void addPost(Post post) {


At this point, you have completed the POST methods. Next, we are going to write the Update and Delete methods

0 0 vote
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
1 year ago

Greetings! This is my 1st comment here so I just wanted to give a quick shout out and tell you I genuinely enjoy reading your articles. Can you recommend any other blogs/websites/forums that cover the same topics? Thanks a lot!

cresent moon cafe
cresent moon cafe
1 year ago

There is certainly a lot to learn about this subject.
I love all of the points you’ve made.