{"id":52,"date":"2019-02-22T23:20:41","date_gmt":"2019-02-22T23:20:41","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/spring-boot\/?p=52"},"modified":"2019-03-02T04:06:56","modified_gmt":"2019-03-02T04:06:56","slug":"04-spring-boot-add-rest-endpoint","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/spring-boot\/04-spring-boot-add-rest-endpoint\/","title":{"rendered":"Spring Boot &#8211; Add REST Endpoint"},"content":{"rendered":"<p>In this tutorial, we are going to be adding a REST Endpoint to our Spring application.<\/p>\n<p><strong>Now, what is a REST Endpoint?<\/strong><\/p>\n<p>A REST endpoint makes it possible for a web application to respond to HTTP REST requests. Therefore, if a user goes to the browser and enters a url, we want to tell spring to run a specific method and send the return value to the user.<\/p>\n<p>&nbsp;<\/p>\n<h4>These are the Steps to Follow<\/h4>\n<p>There are six simple steps to follows. They are listed and explained below.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 1: Add the\u00a0starter-web dependency<\/strong><\/p>\n<p>This dependency allows your application to function as a web application. If you add this dependency, then when you run your application, a tomcat server will be started and your application is automatically deployed.<\/p>\n<p>To add this starter web dependency, copy and paste this code below in your pom.xml file<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;dependency&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;groupId&gt;<\/span>org.springframework.boot<span style=\"color: #007700;\">&lt;\/groupId&gt;<\/span> \r\n    <span style=\"color: #007700;\">&lt;artifactId&gt;<\/span>spring-boot-starter-web<span style=\"color: #007700;\">&lt;\/artifactId&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;version&gt;<\/span>2.1.2.RELEASE<span style=\"color: #007700;\">&lt;\/version&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/dependency&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2: Add the spring webmvc dependency<\/strong><\/p>\n<p>This dependency is used to make your application follow the mvc design pattern. To add the spring webmvc dependency, copy and paste the code below into your pom.xml.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;dependency&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;groupId&gt;<\/span>org.springframework<span style=\"color: #007700;\">&lt;\/groupId&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;artifactId&gt;<\/span>spring-webmvc<span style=\"color: #007700;\">&lt;\/artifactId&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;version&gt;<\/span>5.1.3.RELEASE<span style=\"color: #007700;\">&lt;\/version&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/dependency&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Note<\/strong>: You can find any dependency you want in <a href=\"https:\/\/mvnrepository.com\/\" target=\"_blank\" rel=\"noopener\">Maven Repository<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 3: Create a class (simple .java file)<\/strong><\/p>\n<p>You can create this class in the same package as the base class. For me, this i called the class HomeController.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 4: Annotate the class with <em>@RestController<\/em><\/strong><\/p>\n<p>You use the <em>@RestController<\/em> annotation to tell Spring that this class contains methods that would respond to incoming REST HTTP request.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 5: Write the method you want to run<\/strong><\/p>\n<p>We write a simple method to return a string. The default request method is GET. The whole content of the class file is given below:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">package<\/span> com<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">kindsonthegenius<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">demo<\/span><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.RequestMapping<\/span><span style=\"color: #333333;\">;<\/span>\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;\">HomeController<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\r\n\t<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;\">\"\/\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> String <span style=\"color: #0066bb; font-weight: bold;\">Welcome<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"Welcome to Spring Boot \\n\"<\/span> <span style=\"color: #333333;\">+<\/span>\r\n\t\t<span style=\"background-color: #fff0f0;\">\"Remember to subscribe and leave a comment\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 6: Annotate the method with <em>@RequestMapping<\/em><\/strong><\/p>\n<p>The <em>@RequestMapping<\/em>\u00a0 maps a particular url pattern to a method. In our example, the url pattern is &#8220;\/&#8221; and it maps to the method we just wrote. So when a user makes http request to the &#8220;\/&#8221; url, it executes the method we wrote.<\/p>\n<p>You an watch the complete video explanation of this procedure below:<\/p>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/HOO0MhfVuSA\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/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 are going to be adding a REST Endpoint to our Spring application. Now, what is a REST Endpoint? A REST endpoint &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":55,"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-52","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\/52","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=52"}],"version-history":[{"count":3,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":57,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/52\/revisions\/57"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media\/55"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}