{"id":110,"date":"2019-03-09T12:25:35","date_gmt":"2019-03-09T12:25:35","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/spring-boot\/?p=110"},"modified":"2020-07-26T08:14:07","modified_gmt":"2020-07-26T08:14:07","slug":"13-spring-boot-write-delete-methods","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/spring-boot\/13-spring-boot-write-delete-methods\/","title":{"rendered":"Spring Boot &#8211; Write DELETE Methods"},"content":{"rendered":"<p>In this chapter, we would write the DELETE methods. It is similar to the GET method. However, in case of he DELETE, the nothing is returned. Therefore it is a void method. Also the DELETE method in the controller files takes a path variable.<\/p>\n<ol>\n<li><a href=\"https:\/\/kindsonthegenius.com\/spring-boot\/spring-boot-write-update-methods\/#t1\">Write the deleteLocation Methods<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/spring-boot\/spring-boot-write-update-methods\/#t2\">Write the deleteUser Methods<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/spring-boot\/spring-boot-write-update-methods\/#t3\">Write the deletePost Methods<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/spring-boot\/spring-boot-write-update-methods\/#t4\">Test the Application<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Write the deleteLocation Methods<\/strong><\/h4>\n<p>Open the LocationController file and write the deleteLocation 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;\">\"\/location\/{id}\"<\/span><span style=\"color: #333333;\">,<\/span> method <span style=\"color: #333333;\">=<\/span> RequestMethod<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">DELETE<\/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;\">deleteLocation<\/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: #333333;\">{<\/span>\r\n     locationService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">deleteLocation<\/span><span style=\"color: #333333;\">(<\/span>id<span style=\"color: #333333;\">);<\/span>\r\n <span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.0: deleteLocation 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 deleteLocation 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;\">deleteLocation<\/span><span style=\"color: #333333;\">(<\/span>String id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    locations<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">removeIf<\/span><span style=\"color: #333333;\">(<\/span>t <span style=\"color: #333333;\">-&gt;<\/span> t<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>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.1: deleteLocation for the LocationService class<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Write the deleteUser Methods<\/strong><\/h4>\n<p>Open the UserController file and write the deleteUser 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;\">DELETE<\/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;\">deleteUser<\/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: #333333;\">{<\/span>\r\n      userService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">deleteUser<\/span><span style=\"color: #333333;\">(<\/span>id<span style=\"color: #333333;\">);<\/span>\r\n <span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.2: deleteUser for the UserController class<\/p>\n<p>&nbsp;<\/p>\n<p>Similarly, add the deleteUser 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;\">deleteUser<\/span><span style=\"color: #333333;\">(<\/span>String id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n     users<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">removeIf<\/span><span style=\"color: #333333;\">(<\/span>u <span style=\"color: #333333;\">-&gt;<\/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>\t\t\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.3: deleteUser for the UserService class<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Write the deletePost Methods<\/strong><\/h4>\n<p>Open the PostController file and write the updatedPost method as shown below.<\/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;\">DELETE<\/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;\">deletePost<\/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: #333333;\">{<\/span>\r\n     postService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">deletePost<\/span><span style=\"color: #333333;\">(<\/span>id<span style=\"color: #333333;\">);<\/span>\r\n <span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.4: deletePost for the PostController class<\/p>\n<p>&nbsp;<\/p>\n<p>Similarly, add the deletePost 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;\">deletePost<\/span><span style=\"color: #333333;\">(<\/span>String id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n     posts<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">removeIf<\/span><span style=\"color: #333333;\">(<\/span>p <span style=\"color: #333333;\">-&gt;<\/span> p<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>\t\t\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.5: deletePost 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 GET methods. But this time you need to specify the id in the url as well.<\/p>\n<p>After making the delete, you can then make a post request to confirm the delete worked.<\/p>\n<p>I recommend you watch the video.<\/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 would write the DELETE methods. It is similar to the GET method. However, in case of he DELETE, the nothing is &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":112,"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-110","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\/110","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=110"}],"version-history":[{"count":5,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/110\/revisions"}],"predecessor-version":[{"id":238,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/110\/revisions\/238"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media\/112"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media?parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/categories?post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/tags?post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}