{"id":76,"date":"2019-02-28T06:34:16","date_gmt":"2019-02-28T06:34:16","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/spring-boot\/?p=76"},"modified":"2019-03-02T04:07:54","modified_gmt":"2019-03-02T04:07:54","slug":"08-spring-boot-add-rest-controllers","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/spring-boot\/08-spring-boot-add-rest-controllers\/","title":{"rendered":"Spring Boot &#8211; Add REST Controllers"},"content":{"rendered":"<p>In this lesson, we are going to add REST controllers to our application. Remember that in the last tutorial, we added a business service.<\/p>\n<p>Now the question is: What is a REST Controller? Let&#8217;s see<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li><a href=\"#t1\">What is a Rest Controller<\/a><\/li>\n<li><a href=\"#t2\">Add the User Controller<\/a><\/li>\n<li><a href=\"#t3\">Add the Post Controller<\/a><\/li>\n<li><a href=\"#t4\">Add the Location Controller<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. What is a REST Controller?<\/strong><\/h4>\n<p>A REST is a java class that is annotated with the @RestController annotation. But what does a REST Controller do? A REST controller contains the function that executes when a url request is received.<\/p>\n<p>So a REST controller is the endpoint that receives requests coming from a client. Additionally, a REST controller the code that sends back a response to the client after the request is executed.<\/p>\n<p>Finally, a REST controller provides mappings that map particular url patterns to methods defined in the controller<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Add the User Controller<\/strong><\/h4>\n<p>You need to add a controller that responds to user-related requests. For example, request for list of users. Or maybe request for a single user. Or even to add or delete a user.<\/p>\n<p>Add a new file to to the users package. Name it UserController. I&#8217;ve written the content out for you. So you can copy and paste.<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">package<\/span> user<span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">org.springframework.web.bind.annotation.RestController<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #555555; font-weight: bold;\">@RestController<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">UserController<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Add the Post Controller<\/strong><\/h4>\n<p>You need to add a controller that responds to post-related requests. For example, request for list of posts. Or maybe request for posts by user or by date. Or even to add or delete a post.<\/p>\n<p>Add a new file to to the post package. Name it PostController. I&#8217;ve written the content out for you. So you can copy and paste.<\/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;\">package<\/span> post<span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">org.springframework.web.bind.annotation.RestController<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #555555; font-weight: bold;\">@RestController<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">PostController<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Add the Location Controller<\/strong><\/h4>\n<p>You need to add a controller that responds to location-related requests. For example, request for list of Locations. Or maybe request for a single location. Or even to add or delete a location.<\/p>\n<p>Add a new file to to the location package. Name it LocationController. I&#8217;ve written the content out for you. So you can copy and paste.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">package<\/span> location<span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">org.springframework.web.bind.annotation.RestController<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #555555; font-weight: bold;\">@RestController<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">LocationController<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\r\n\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In the next tutorial, we would then add logic for all these 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 are going to add REST controllers to our application. Remember that in the last tutorial, we added a business service. Now &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":77,"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":[10],"class_list":["post-76","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring-boot-tutorials","tag-spring-boot-tutorials"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/76","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=76"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":79,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/76\/revisions\/79"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media\/77"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}