{"id":62,"date":"2019-02-24T23:00:12","date_gmt":"2019-02-24T23:00:12","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/spring-boot\/?p=62"},"modified":"2019-03-02T04:07:16","modified_gmt":"2019-03-02T04:07:16","slug":"06-spring-boot-build-a-rest-api","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/spring-boot\/06-spring-boot-build-a-rest-api\/","title":{"rendered":"Spring Boot &#8211; Build a REST API"},"content":{"rendered":"<p>In this tutorial, we would would outline the steps to Build a REST API. We would also plan the various components of out REST API.<\/p>\n<p>As a reminder, an API(Application Programming Interface) allows exposes functionality of an application to other applications. So as a programmer, you&#8217;ll spend most of your time building APIs.<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li><a href=\"#t1\">About the API we&#8217;ll Build<\/a><\/li>\n<li><a href=\"#t2\">Create the User&#8217;s Class<\/a><\/li>\n<li><a href=\"#t3\">Create the Location Class<\/a><\/li>\n<li><a href=\"#t4\">Create the Post Class<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=Je5Ffk73K0A\" target=\"_blank\" rel=\"noopener\">Watch the Video<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. About the API we&#8217;ll Build<\/strong><\/h4>\n<p>So we would build an API for a social network platform. Let&#8217;s call it SocialAPI. This is like a mini Facebook! Although it is small, this is the same principle social networks follow.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Our SocialAPI would contain the following classes:<\/strong><\/p>\n<ul>\n<li>list of users<\/li>\n<li>list of locations<\/li>\n<li>list of posts<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Also, the following relationships would be supported:<\/strong><\/p>\n<ul>\n<li>a user has a location<\/li>\n<li>each location could have 1 or more users<\/li>\n<li>a user could create a post. Hence each user can have one or more posts<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Our SocialAPI would support the following operations:<\/strong><\/p>\n<ul>\n<li>add, delete and update user details<\/li>\n<li>add, delete and update location details<\/li>\n<li>add, delete and update post details<\/li>\n<li>get a list of users or locations<\/li>\n<li>get user or location by id<\/li>\n<li>get list of posts for a particular user<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>The entity relationship diagram for our SocialAPI is shown below.<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_63\" aria-describedby=\"caption-attachment-63\" style=\"width: 537px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2019\/02\/Database-Diagram.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-63\" src=\"https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2019\/02\/Database-Diagram.jpg\" alt=\"\" width=\"537\" height=\"347\" srcset=\"https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2019\/02\/Database-Diagram.jpg 852w, https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2019\/02\/Database-Diagram-300x194.jpg 300w, https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2019\/02\/Database-Diagram-768x497.jpg 768w\" sizes=\"auto, (max-width: 537px) 100vw, 537px\" \/><\/a><figcaption id=\"caption-attachment-63\" class=\"wp-caption-text\">Database Diagram for our Social API<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Create the Users Class<\/strong><\/h4>\n<ul>\n<li>First you need to create a package that would contain the User class<\/li>\n<li>Then, inside package create a class and call it user<\/li>\n<li>The content of the User class is shown below. So you can copy and paste. You can also type it out yourself<\/li>\n<\/ul>\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> user<span style=\"color: #333333;\">;<\/span>\r\n\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;\">User<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> String id<span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> String firstname<span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> String lastname<span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> Location location<span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> String email<span style=\"color: #333333;\">;<\/span>\r\n\t\t\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>After you have added this code, then you can generate the getters and setters.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Create the Post Class<\/strong><\/h4>\n<ul>\n<li>First you need to create a package that would contain the Post class<\/li>\n<li>Then, inside the course package create a class and call it Post<\/li>\n<li>The content of the Post class is shown below. So you can copy and paste. You can also type it out yourself<\/li>\n<\/ul>\n<p>&nbsp;<\/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;\">user.User<\/span><span style=\"color: #333333;\">;<\/span>\r\n\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;\">Post<\/span> <span style=\"color: #333333;\">{<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">private<\/span> String id<span style=\"color: #333333;\">;<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">private<\/span> String postdate<span style=\"color: #333333;\">;<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">private<\/span> User user<span style=\"color: #333333;\">;<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">private<\/span> String details<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Again, remember to generate the getters and setters for this the User class<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Create the Location Class<\/strong><\/h4>\n<p>It follows the same process<\/p>\n<ul>\n<li>First you need to create a package that would contain the Location class<\/li>\n<li>Then inside the location package create a class and call it Location<\/li>\n<li>The content of the Location class is shown below. So you can copy and paste. You can also type it out yourself<\/li>\n<\/ul>\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> location<span style=\"color: #333333;\">;<\/span>\r\n\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;\">Location<\/span> <span style=\"color: #333333;\">{<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">private<\/span> String id<span style=\"color: #333333;\">;<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">private<\/span> String name<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Also remember to generate getters and setters. You can watch the video below to see how you can do this.<\/p>\n<p>&nbsp;<\/p>\n<p>So at this point, you have successfully set up the structure of the SocialAPI. So in the next lesson, we would add service to get list of items.<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/Je5Ffk73K0A\" width=\"100%\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\">\ufeff<\/span><\/iframe><\/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 tutorial, we would would outline the steps to Build a REST API. We would also plan the various components of out REST API. &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":64,"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-62","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\/62","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=62"}],"version-history":[{"count":5,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":69,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/62\/revisions\/69"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media\/64"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}