# Developing REST Service using Spring Boot

##  Overview
In this tutorial, we'll learn, how to develop a REST API using the Spring Boot framework. 

We’ll primarily use **Java configuration** in this tutorial.

## Pre-requisties
* JDK 1.8
* Maven
* IDE (for ease of use)

## Setup maven project

 Create a maven project and add the required dependencies and plugins in pom.xml

We will use the dependencyManagement to define a standard version of an artifact to use across multiple projects. 

We will allow **spring-boot-starter-parent** to manage the version of the artifacts.

``` xml
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
```

Now we'll add the spring-boot-starter-web dependency.

``` xml
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
```

**Spring Boot Maven Plugin** provides Spring Boot support in Maven, letting you package executable jar or war archives and run an application “in-place”. 

``` xml
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
        </plugin>                     
    </plugins>
</build>
```

### Creating a Spring Boot Application
Create a class with the main method. Annotate the class with @SpringBootApplication. Call the run method of **SpringApplication class** 

``` java
package com.tutorialflix.spring.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootWebApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootWebApplication.class, args);
	}
}
```

**@SpringBootApplication** includes the functionality of following annotations:
* @Configuration 
* @EnableAutoConfiguration
* @ComponentScan.

** By default, it will perform the component scan in the base package. So make sure <u>SpringBootWebApplication</u> class is present in the base package.**

### Creating a REST Controller 
Create a class with a meaningful name based on your requirement & annotate the class with **@RestController**. **The  @RestController  is the central artifact in the entire Web Tier of the RESTful API.** 

``` java
package com.tutorialflix.spring.web.contrroller;

import org.springframework.stereotype.RestController;

@RestController
public class MyRestController {

	
}
```

### Create a GET method to accept requests
Create a method named home to accept the request on '/' URL and show the index.html

``` java

	@GetMapping("/sayHello")
	public Map<String,String> sayHello() {
		Map map = new HashMap<>();
		map.put("response","Hello, how are you");
		return map;

```


### Create a POST method to accept requests
Create a method named home to accept the request on '/' URL and show the index.html

``` java
	@PostMapping("/sayHelloWithName")
	public Map<String,String> sayHelloWithName(Map<String,String> request) 	{
		Map map = new HashMap<>();
		map.put("response","Hello, how are you " + request.get("name"));
		return map;
	}


```

### Configuration using properties file

Create a file **application.properties**  inside **resources** folder of the application and following properties.

``` 
server.port=8081
logging.level.org.springframework.web=DEBUG
```

**server.port** : to change the default port 8080 to any other port number.

**logging.level.org.springframework.web** : to change the log level default INFO to DEBUG

## Run the REST API

``` bash
$ mvn spring-boot:run
```

## Test

Goto http://localhost:8081/
