<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Suyash Mittal</title>
    <description>The latest articles on DEV Community by Suyash Mittal (@suyash01).</description>
    <link>https://dev.to/suyash01</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F229544%2Fbf3cbb50-7ae7-423d-b9b1-63dd59546faf.jpeg</url>
      <title>DEV Community: Suyash Mittal</title>
      <link>https://dev.to/suyash01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/suyash01"/>
    <language>en</language>
    <item>
      <title>Getting Started with Spring Boot 2</title>
      <dc:creator>Suyash Mittal</dc:creator>
      <pubDate>Sun, 15 Sep 2019 07:34:55 +0000</pubDate>
      <link>https://dev.to/suyash01/getting-started-with-spring-boot-2-3doi</link>
      <guid>https://dev.to/suyash01/getting-started-with-spring-boot-2-3doi</guid>
      <description>&lt;p&gt;The prerequisite of this tut will be &lt;strong&gt;Java&lt;/strong&gt;, some knowledge about &lt;strong&gt;Maven&lt;/strong&gt; and  &lt;strong&gt;Spring&lt;/strong&gt; and how to use &lt;strong&gt;Spring Tool Suite 4&lt;/strong&gt; (or Eclipse).&lt;/p&gt;

&lt;p&gt;We will create a REST API using &lt;a href="https://spring.io/projects/spring-boot" rel="noopener noreferrer"&gt;Spring Boot&lt;/a&gt;. I will be using &lt;a href="https://spring.io/tools" rel="noopener noreferrer"&gt;Spring Tool Suite 4&lt;/a&gt; and JDK 1.8 for the project. The project can be generated from &lt;a href="https://start.spring.io/" rel="noopener noreferrer"&gt;Spring Initializr&lt;/a&gt; website or directly from the STS4, both of which have been mentioned below. The project will use &lt;a href="https://maven.apache.org/" rel="noopener noreferrer"&gt;Maven&lt;/a&gt; for dependency management.&lt;/p&gt;

&lt;p&gt;If you need a custom group name and artifact id go ahead and fill it but it is not mandatory. You can also use what I have used in the images.&lt;/p&gt;

&lt;p&gt;For this project, we will only require the web dependency provided by the spring boot starter.&lt;/p&gt;

&lt;h5&gt;
  
  
  Spring Initializr
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9dbds9qzs6x6mkftcefo.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9dbds9qzs6x6mkftcefo.jpg" alt="Spring Initializr" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Search for the web dependency in the dependency search bar and select it as shown and then click on the &lt;strong&gt;Generate the project&lt;/strong&gt; button. You will get a zip file which you need to extract and import into STS.&lt;/p&gt;

&lt;h5&gt;
  
  
  STS4
&lt;/h5&gt;

&lt;p&gt;In STS create a Spring Starter Project and fill out the details as you like or you can use the details in the above picture.&lt;/p&gt;

&lt;p&gt;Click on next and then search for the dependency named &lt;strong&gt;Spring Web&lt;/strong&gt;, select it and click on finish.&lt;/p&gt;

&lt;h5&gt;
  
  
  Coding
&lt;/h5&gt;

&lt;p&gt;Now that you have everything set up create a package named &lt;strong&gt;controller&lt;/strong&gt; under the base package that the starter gave you and then create a class name &lt;strong&gt;RestExampleController&lt;/strong&gt; in that package. The project structure will look as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwlk85v2o2j5qoli24atw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwlk85v2o2j5qoli24atw.jpg" alt="Project Structure" width="349" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the only file that we need to create for handling our request. The final code for the controller class is given below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;me.suyashmittal.restexample.controller&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.web.bind.annotation.GetMapping&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.web.bind.annotation.PostMapping&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.web.bind.annotation.RequestBody&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.web.bind.annotation.RequestMapping&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.web.bind.annotation.RequestMethod&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.web.bind.annotation.RequestParam&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.web.bind.annotation.RestController&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RestExampleController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@RequestMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestParam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;defaultValue&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"anonymous"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@RequestMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"/getRequest/{name}"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RequestMethod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;GET&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getIndex&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@PathVariable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/get"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getRequest&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hello from /get"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@PostMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/post"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;postRequest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Understanding
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;@RestController&lt;/code&gt; annotation is used to tell the framework that it should be included in the component scan and the return value of each mapping should be directly sent as the response.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@RequestMapping&lt;/code&gt; annotation is used to map the request to the mentioned endpoint for example here the endpoints are &lt;code&gt;/&lt;/code&gt; and &lt;code&gt;/getRequest&lt;/code&gt;. It handles all the types of request which is not ideal in all the scenarios therefore, we can specify the type of request using the &lt;code&gt;method&lt;/code&gt; parameter as shown above the 2nd method.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@GetMapping&lt;/code&gt; and &lt;code&gt;@PostMapping&lt;/code&gt; are special forms of &lt;code&gt;@RequestMapping&lt;/code&gt; where the method is already specified which are &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt; respectively. Spring Boot also provides similar annotations for other Http methods like &lt;code&gt;PUT&lt;/code&gt;, &lt;code&gt;DELETE&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@RequestParam&lt;/code&gt; is used to fetch the parameters from the &lt;code&gt;url&lt;/code&gt;, I have also specified a default value which is not mandatory. For example &lt;code&gt;http://localhost:8080/?name=suyash&lt;/code&gt; will result in &lt;code&gt;Hello suyash&lt;/code&gt;. You can also set the &lt;code&gt;required&lt;/code&gt; parameter to make the param mandatory or vice versa.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@PathVariable&lt;/code&gt; is used to extract a part of the &lt;code&gt;url&lt;/code&gt;. For example &lt;code&gt;http://localhost:8080/getRequest/suyash&lt;/code&gt; will result in &lt;code&gt;Hello suyash&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@RequestBody&lt;/code&gt; is used to fetch the body in case of &lt;code&gt;POST&lt;/code&gt; and &lt;code&gt;PUT&lt;/code&gt; request. By default, the value must be present in the body but it can be made optional using the &lt;code&gt;required&lt;/code&gt; parameter. Complex objects can be consumed by building a class.&lt;/p&gt;

&lt;h5&gt;
  
  
  Running
&lt;/h5&gt;

&lt;p&gt;You can run the project by right click on the project name and then run as the Spring Boot App. By default, the server will start on port &lt;code&gt;8080&lt;/code&gt; and now you can experiment with the app using any REST client such as Postman.&lt;/p&gt;

&lt;p&gt;TIP: You can change the port of the server by defining &lt;code&gt;server.port=9000&lt;/code&gt; (if you want to run on port 9000) property in the &lt;code&gt;application.properties&lt;/code&gt; file present in the &lt;code&gt;resources&lt;/code&gt; folder.&lt;/p&gt;

&lt;h5&gt;
  
  
  Note from author
&lt;/h5&gt;

&lt;p&gt;This is my first article, any suggestion, recommendation or correction is heartily welcomed.&lt;/p&gt;

&lt;p&gt;If you have any doubts you can comment below, I will try my best to resolve them.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>java</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
