Spring Boot | Sample REST Application
In this post we are going to write a Simple REST Application in Spring Boot. Spring Boot has inbuilt tomcat which makes an application start without the need to have any application/web server. Though we can always deploy applications in web servers too.
For this tutorial we will be using Maven for dependency management.
Step 1. Add all required Spring dependencies in your pom file
We can use below sample pom file to start with or go to https://start.spring.io/ to kick start your spring boot application.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.tutorials.java</groupId> | |
<artifactId>SpringBootHelloWorld</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>SpringBootDemo</name> | |
<description>Spring Boot Demo</description> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.4.0.RELEASE</version> | |
<relativePath/> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-jpa</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-hateoas</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-jersey</artifactId> | |
</dependency> | |
<!--<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-security</artifactId> | |
</dependency>--> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.hsqldb</groupId> | |
<artifactId>hsqldb</artifactId> | |
<scope>runtime</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Step 2. Spring starter class
Spring Boot uses @SpringBootApplication to auto configure some of spring configuration. You can find more info on SpringBoot details
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tutorials.java; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
@SpringBootApplication | |
public class SpringBootDemoApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(SpringBootDemoApplication.class, args); | |
} | |
} |
Step 3. Application Controller
We will be using MVC pattern to separate our restful layer from the service layer.
In our REST service we use @RestController annotation. It is combination of @Controller and @ResponseBody annotations.
We created CustomerController to return customer details.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tutorials.java.controller; | |
import java.util.List; | |
import com.tutorials.java.ICustomer; | |
import com.tutorials.java.service.CustomerService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import com.tutorials.java.model.Customer; | |
@RestController | |
@RequestMapping("/customer") | |
public class CustomerController implements ICustomer { | |
@Autowired | |
private ICustomer customerService; | |
@Override | |
@RequestMapping | |
public List<Customer> getAllCustomers() { | |
return customerService.getAllCustomers(); | |
} | |
@Override | |
@RequestMapping("/{id}") | |
public Customer getCustomerById(@PathVariable String id) { | |
return customerService.getCustomerById(id); | |
} | |
} |
Step 4. Application Service
In any application, the service takes a request from the endpoint and processes it and gives back a response.
In our application we will be returning mocked data for each request. In the real scenario this generally calls any other service (third-party etc), db using a repository to fetch data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tutorials.java.service; | |
import com.tutorials.java.ICustomer; | |
import com.tutorials.java.model.Customer; | |
import org.springframework.stereotype.Component; | |
import java.util.ArrayList; | |
import java.util.List; | |
@Component | |
public class CustomerService implements ICustomer { | |
@Override | |
public List<Customer> getAllCustomers() { | |
List<Customer> listOfCustomers = new ArrayList(); | |
Customer rohan = new Customer(1, "Rohan", "Lopes", "abc@abc.com"); | |
Customer nikhil = new Customer(2, "Nikhil", "Lopes", "xyz@abc.com"); | |
listOfCustomers.add(rohan); | |
listOfCustomers.add(nikhil); | |
return listOfCustomers; | |
} | |
@Override | |
public Customer getCustomerById(String id) { | |
Customer rohan = new Customer(1, "Rohan", "Lopes", "abc@abc.com"); | |
return rohan; | |
} | |
} |
Step 5. Disable Spring security
As this is a demo application, we will be disabling the spring authentication. To do that, we will be adding security.basic.enabled=false to the application.properties file.
Step 6. Test Application
Once all changes added we can run command mvn clean install to build our application and create jar file.
Run this command to start application java -jar target\SpringBootHelloWorld-0.0.1-SNAPSHOT.jar
Comments
Post a Comment