{"id":106,"date":"2019-03-09T11:28:46","date_gmt":"2019-03-09T11:28:46","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/spring-boot\/?p=106"},"modified":"2019-03-12T14:23:06","modified_gmt":"2019-03-12T14:23:06","slug":"13-spring-boot-write-update-methods","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/spring-boot\/13-spring-boot-write-update-methods\/","title":{"rendered":"Spring Boot &#8211; Write UPDATE Methods"},"content":{"rendered":"<p>In this chapter, we are going to write the Update Methods (PUT) to update records.<\/p>\n<p>So this follows from the same procedure as with the POST methods.<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li><a href=\"#t1\">Write the updateLocation Methods<\/a><\/li>\n<li><a href=\"#t2\">Write the updateUser Methods<\/a><\/li>\n<li><a href=\"#t3\">Write the updatePost Methods<\/a><\/li>\n<li><a href=\"#t4\">Test the Application<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Write the updateLocation Methods<\/strong><\/h4>\n<p>Open the LocationController file and write the updateLocation method as shown below<\/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;\">\"\/locations\/{id}\"<\/span><span style=\"color: #333333;\">,<\/span> method <span style=\"color: #333333;\">=<\/span> RequestMethod<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">PUT<\/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;\">updateLocation<\/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: #555555; font-weight: bold;\">@PathVariable<\/span> String id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{\r\n<\/span>\r\n     locationService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">updateLocation<\/span><span style=\"color: #333333;\">(<\/span>id<span style=\"color: #333333;\">,<\/span> location<span style=\"color: #333333;\">);\r\n<\/span>\r\n<span style=\"color: #333333;\">}<\/span><\/pre>\n<p>Listing 1.0:updateLocation for the LocaitonController<\/p>\n<p>&nbsp;<\/p>\n<p>After you write this code, you will have an error. So you need to\u00a0add the updateLocation method in the LocationService file as shown 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;\">updateLocation<\/span><span style=\"color: #333333;\">(<\/span>String id<span style=\"color: #333333;\">,<\/span> Location location<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{\r\n<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> i <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span> i <span style=\"color: #333333;\">&lt;<\/span> locations<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">size<\/span><span style=\"color: #333333;\">();<\/span> i<span style=\"color: #333333;\">++)<\/span> <span style=\"color: #333333;\">{\r\n<\/span>\r\n\tLocation l <span style=\"color: #333333;\">=<\/span> locations<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">get<\/span><span style=\"color: #333333;\">(<\/span>i<span style=\"color: #333333;\">);\r\n<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>l<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getId<\/span><span style=\"color: #333333;\">().<\/span><span style=\"color: #0000cc;\">equals<\/span><span style=\"color: #333333;\">(<\/span>id<span style=\"color: #333333;\">))<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t    locations<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">set<\/span><span style=\"color: #333333;\">(<\/span>i<span style=\"color: #333333;\">,<\/span> location<span style=\"color: #333333;\">);<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n   <span style=\"color: #333333;\">}<\/span>\t\t\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.1: updateLocation for the LocationService class<\/p>\n<p>&nbsp;<\/p>\n<p>In the code above, we simply loop through the collection to find a matching id. Then we replace location at that position with the passed in Location.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Write the updateUser Methods<\/strong><\/h4>\n<p>Open the UserController file and write the updateUser method as shown below<\/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\/{id}\"<\/span><span style=\"color: #333333;\">,<\/span> method <span style=\"color: #333333;\">=<\/span> RequestMethod<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">PUT<\/span><span style=\"color: #333333;\">)\r\n<\/span><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;\">getUser<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@PathVariable<\/span> String id<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;\">{\r\n<\/span>\r\n    userService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">updateUser<\/span><span style=\"color: #333333;\">(<\/span>id<span style=\"color: #333333;\">,<\/span> user<span style=\"color: #333333;\">);\r\n<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.2: updateUser for the UserController class<\/p>\n<p>&nbsp;<\/p>\n<p>Similarly, add the updateUser method in the UserService file as shown 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;\">updateUser<\/span><span style=\"color: #333333;\">(<\/span>String id<span style=\"color: #333333;\">,<\/span> User user<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{\r\n<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> i <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span> i <span style=\"color: #333333;\">&lt;<\/span> users<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">size<\/span><span style=\"color: #333333;\">();<\/span> i<span style=\"color: #333333;\">++)<\/span> <span style=\"color: #333333;\">{\r\n<\/span>\r\n\tUser u <span style=\"color: #333333;\">=<\/span> users<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">get<\/span><span style=\"color: #333333;\">(<\/span>i<span style=\"color: #333333;\">);\r\n<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>u<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getId<\/span><span style=\"color: #333333;\">().<\/span><span style=\"color: #0000cc;\">equals<\/span><span style=\"color: #333333;\">(<\/span>id<span style=\"color: #333333;\">))<\/span> <span style=\"color: #333333;\">{\r\n<\/span>\r\n\t     users<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">set<\/span><span style=\"color: #333333;\">(<\/span>i<span style=\"color: #333333;\">,<\/span> user<span style=\"color: #333333;\">);\r\n<\/span>\r\n\t     <span style=\"color: #008800; font-weight: bold;\">return<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.3:updateUser for the UserService class<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Write the updatePost Methods<\/strong><\/h4>\n<p>Open the PostController file and write the updatedPost method as shown below.<\/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\/{id}\"<\/span><span style=\"color: #333333;\">,<\/span> method <span style=\"color: #333333;\">=<\/span> RequestMethod<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">PUT<\/span><span style=\"color: #333333;\">)\r\n<\/span><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;\">updatePost<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@PathVariable<\/span> String id<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;\">{\r\n<\/span>\r\n      postService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">updatePost<\/span><span style=\"color: #333333;\">(<\/span>id<span style=\"color: #333333;\">,<\/span> post<span style=\"color: #333333;\">);\r\n<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.4: updatePost for the PostController class<\/p>\n<p>&nbsp;<\/p>\n<p>Similarly, add the updatePost method in the LocationService file as shown below<br \/>\n<!-- 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;\">updatePost<\/span><span style=\"color: #333333;\">(<\/span>String id<span style=\"color: #333333;\">,<\/span> Post post<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> i <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span> i <span style=\"color: #333333;\">&lt;<\/span> posts<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">size<\/span><span style=\"color: #333333;\">();<\/span> i<span style=\"color: #333333;\">++)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n\tPost p <span style=\"color: #333333;\">=<\/span> posts<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">get<\/span><span style=\"color: #333333;\">(<\/span>i<span style=\"color: #333333;\">);<\/span>\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>p<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">equals<\/span><span style=\"color: #333333;\">(<\/span>post<span style=\"color: #333333;\">))<\/span> <span style=\"color: #333333;\">{\r\n<\/span>\r\n\t     posts<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">set<\/span><span style=\"color: #333333;\">(<\/span>i<span style=\"color: #333333;\">,<\/span> post<span style=\"color: #333333;\">);<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.5:updatePost for the postService class<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Test the Application<\/strong><\/h4>\n<p>Now, you can use Advanced REST Client to test the update methods.<\/p>\n<p>You do it almost the same way as the POST methods. But this time you need to specify the id in the url as well.<\/p>\n<p>I recommend you watch the video.<\/p>\n<p>Next, we would write the 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 chapter, we are going to write the Update Methods (PUT) to update records. So this follows from the same procedure as with the &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":108,"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-106","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\/106","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=106"}],"version-history":[{"count":3,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/106\/revisions"}],"predecessor-version":[{"id":111,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/106\/revisions\/111"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media\/108"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media?parent=106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/categories?post=106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/tags?post=106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}