{"id":95,"date":"2019-03-07T12:51:44","date_gmt":"2019-03-07T12:51:44","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/spring-boot\/?p=95"},"modified":"2020-07-26T08:13:13","modified_gmt":"2020-07-26T08:13:13","slug":"12-spring-boot-write-post-methods","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/spring-boot\/12-spring-boot-write-post-methods\/","title":{"rendered":"Spring Boot &#8211; Write POST Methods"},"content":{"rendered":"<p>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.<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li><a href=\"#t1\">Create New Location<\/a><\/li>\n<li><a href=\"#t2\">Install Advanced REST Client<\/a><\/li>\n<li><a href=\"#t3\">Test the POST Method<\/a><\/li>\n<li><a href=\"#t4\">Create new User<\/a><\/li>\n<li><a href=\"#t5\">Create new Post<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id = \"t1\">1. Create New Location<\/strong><\/h4>\n<p>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.<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@RequestMapping<\/span>(method=RequestMethod.<span style=\"color: #0000cc;\">POST<\/span>, value=<span style=\"background-color: #fff0f0;\">\"\/locations\"<\/span>)\r\n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">addLocation<\/span>() {\r\n\t\t\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>You can see that the url for inserting a new location is same as the url for getAllLocations. We used this <a href=\"https:\/\/kindsonthegenius.com\/spring-boot\/09-spring-boot-write-get-methods\/\">when we created the GET methods.<\/a><\/p>\n<p>The complete method is given below:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@RequestMapping<\/span><span style=\"color: #333333;\">(<\/span>method<span style=\"color: #333333;\">=<\/span>RequestMethod<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">POST<\/span><span style=\"color: #333333;\">,<\/span> value<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"\/locations\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">addLocation<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@RequestBody<\/span> Location location<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\tlocationService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addLocation<\/span><span style=\"color: #333333;\">(<\/span>location<span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Now, the addLocation() method have not been implemented in the LocationService. So you need to create this method in the LocationService class.<\/p>\n<p>Put the code below in the LocationService<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">addLocation<\/span><span style=\"color: #333333;\">(<\/span>Location location<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n     locations<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">add<\/span><span style=\"color: #333333;\">(<\/span>location<span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Next, we are going to test the post methods.To do this we would need a tool.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id = \"t2\">2. Install Advanced REST Client<\/strong><\/h4>\n<p>To make a POST request, we need to provide the data we want to insert. Since this is an API, we don&#8217;t have a HTML form to fill in this data.<\/p>\n<p>So to provide input, we need to use a tool called Advanced REST Client. This is a free plugin to Google Chrome.<\/p>\n<p>To install it, visit this link (<a href=\"https:\/\/chrome.google.com\/webstore\/detail\/advanced-rest-client\/hgmloofddffdnphfgcellkdfbfbjeloo\" target=\"_blank\" rel=\"noopener\">Get Advanced REST Client<\/a>) and just install it. Next, launch it. The interface is as shown below. You can watch the video to see the procedure.<\/p>\n<p><a href=\"https:\/\/youtu.be\/KD36Yy9mMUE\" target=\"_blank\" rel=\"noopener\">Watch the video of How to Use Advanced REST Client<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-102 size-full\" src=\"https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2019\/03\/REST-Client.jpg\" alt=\"\" width=\"1010\" height=\"653\" srcset=\"https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2019\/03\/REST-Client.jpg 1010w, https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2019\/03\/REST-Client-300x194.jpg 300w, https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2019\/03\/REST-Client-768x497.jpg 768w\" sizes=\"auto, (max-width: 1010px) 100vw, 1010px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id = \"t3\">3. Test the POST Method<\/strong><\/h4>\n<p>Before you do the test, you need to make a little change to the locations variable in the LocationService class.<\/p>\n<p>Replace this existing location variables,<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">List<span style=\"color: #333333;\">&lt;<\/span>Location<span style=\"color: #333333;\">&gt;<\/span> locations <span style=\"color: #333333;\">=<\/span> Arrays<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">asList<\/span><span style=\"color: #333333;\">(<\/span>location1<span style=\"color: #333333;\">,<\/span> location2<span style=\"color: #333333;\">,<\/span> location3<span style=\"color: #333333;\">);<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>with this one;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">List<span style=\"color: #333333;\">&lt;<\/span>Location<span style=\"color: #333333;\">&gt;<\/span> locations <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> ArrayList<span style=\"color: #333333;\">&lt;&gt;(<\/span>Arrays<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">asList<\/span><span style=\"color: #333333;\">(<\/span>location1<span style=\"color: #333333;\">,<\/span> location2<span style=\"color: #333333;\">,<\/span> location3<span style=\"color: #333333;\">));<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>This would make it possible to modify the ArrayList<\/p>\n<p>Now, test the POST Method using Advanced REST Client.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id = \"t4\">4. Create New User<\/strong><\/h4>\n<p>Write the addUser method in the UserController class.<\/p>\n<p>In the controller file, replace the line:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">private<\/span> List<span style=\"color: #333333;\">&lt;<\/span>User<span style=\"color: #333333;\">&gt;<\/span> users <span style=\"color: #333333;\">=<\/span> Arrays<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">asList<\/span><span style=\"color: #333333;\">(<\/span>user1<span style=\"color: #333333;\">,<\/span> user2<span style=\"color: #333333;\">);<\/span>\t\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>with this one<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">private<\/span> List<span style=\"color: #333333;\">&lt;<\/span>User<span style=\"color: #333333;\">&gt;<\/span> users <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> ArrayList<span style=\"color: #333333;\">&lt;&gt;(<\/span>Arrays<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">asList<\/span><span style=\"color: #333333;\">(<\/span>user1<span style=\"color: #333333;\">,<\/span> user2<span style=\"color: #333333;\">));<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The addUser method for the UserController class is given below. You can copy and paste it.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@RequestMapping<\/span><span style=\"color: #333333;\">(<\/span>value<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"\/users\"<\/span><span style=\"color: #333333;\">,<\/span> method <span style=\"color: #333333;\">=<\/span> RequestMethod<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">POST<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">addUser<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@RequestBody<\/span> User user<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\tuserService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addUser<\/span><span style=\"color: #333333;\">(<\/span>user<span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Finally, place the addUser method for the UserService class<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">addUser<\/span><span style=\"color: #333333;\">(<\/span>User user<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    users<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">add<\/span><span style=\"color: #333333;\">(<\/span>user<span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>You can then test the application using Advanced REST Client. Try to add a new User.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id = \"t5\">5. Create New Post<\/strong><\/h4>\n<p>Finally, we would work on inserting a new Post. As before, replace this line in PostService<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">List<span style=\"color: #333333;\">&lt;<\/span>Post<span style=\"color: #333333;\">&gt;<\/span> posts <span style=\"color: #333333;\">=<\/span> Arrays<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">asList<\/span><span style=\"color: #333333;\">(<\/span>post1<span style=\"color: #333333;\">,<\/span> post2<span style=\"color: #333333;\">);<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>with this one:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">List<span style=\"color: #333333;\">&lt;<\/span>Post<span style=\"color: #333333;\">&gt;<\/span> posts <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> ArrayList<span style=\"color: #333333;\">&lt;&gt;(<\/span>Arrays<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">asList<\/span><span style=\"color: #333333;\">(<\/span>post1<span style=\"color: #333333;\">,<\/span> post2<span style=\"color: #333333;\">));<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The addPost method for the PostController class is as shown below<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@RequestMapping<\/span><span style=\"color: #333333;\">(<\/span>value <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"\/posts\"<\/span><span style=\"color: #333333;\">,<\/span> method <span style=\"color: #333333;\">=<\/span> RequestMethod<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">POST<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">addPost<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@RequestBody<\/span> Post post<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n     postService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addPost<\/span><span style=\"color: #333333;\">(<\/span>post<span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The addPost method for the PostService class is as below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">addPost<\/span><span style=\"color: #333333;\">(<\/span>Post post<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n     posts<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">add<\/span><span style=\"color: #333333;\">(<\/span>post<span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>At this point, you have completed the POST methods. Next, we are going to write the Update and Delete methods<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":104,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-95","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring-boot-tutorials"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/comments?post=95"}],"version-history":[{"count":4,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/95\/revisions"}],"predecessor-version":[{"id":234,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/95\/revisions\/234"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media\/104"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media?parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/categories?post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/tags?post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}