Creating restful web services using Spring Boot in 15 minutes
Restful web services using Spring Boot
We have already created Restful Web Service using Spring. So now its time to learn a new and advanced way to create rest service using Spring Boot. You can go through Spring Boot official document to learn more on Spring Boot. However, In this post, I am going to create a Spring Boot rest application.Before I proceed with application development let me list down the software used for this application.
- Eclipse Oxygen
- Jdk 1.8 - Steps to install jdk
1- Create a Spring Boot application.
- Go to Spring initializer and generate a maven project.
- It will download the maven project in a .zip file.
- Import the maven project in eclipse.
2- Below the code for this rest application.
pom.xml
<?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.employee</groupId>
<artifactId>employee-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>employee-services</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</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>
Create a package structure as shown.
SpringBootWebApplication.java
package com.employee.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan("com.employee")
@SpringBootApplication
public class SpringBootWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
Model: Employee.java
package com.student.model;
import java.io.Serializable;
/**
* @author javainstance
*
*/
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String dept;
public Employee() {
}
public Employee(int id, String name, String dept) {
super();
this.id = id;
this.name = name;
this.dept = dept;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getDept() {
return dept;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", dept=" + dept + "]";
}
}
Controller : EmployeeController.java
package com.employee.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.employee.service.EmployeeService;
import com.student.model.Employee;
/**
* @author javainstance
*
*/
@RestController
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping("/employees")
public List<Employee> getEmployees(){
return employeeService.getEmployees();
}
@GetMapping("/employee/{employeeId}")
public Employee getEmployee(@PathVariable int employeeId){
return employeeService.getEmployee(employeeId);
}
@PostMapping("/employee")
public Employee saveEmployee(@RequestBody Employee employee){
return employeeService.save(employee);
}
}
Service:
EmployeeService.java
package com.employee.service;
import java.util.List;
import com.student.model.Employee;
/**
* @author javainstance
*
*/
public interface EmployeeService {
public List<Employee> getEmployees();
public Employee getEmployee(int studentId);
public Employee save(Employee student);
public Employee updateEmployee();
}
EmployeeServiceImpl.java
package com.employee.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
import com.student.model.Employee;
/**
* @author javainstance
*
*/
@Component
public class EmployeeServiceImpl implements EmployeeService {
private static List<Employee> employeeList =new ArrayList<>();
static {
employeeList.add(new Employee(1,"Shashi","HR"));
employeeList.add(new Employee(2,"Anita","ADMIN"));
employeeList.add(new Employee(3,"Saanvi","TRAVEL"));
}
@Override
public List<Employee> getEmployees() {
return employeeList;
}
@Override
public Employee getEmployee(int id) {
return employeeList.get(id-1);
}
@Override
public Employee save(Employee employee) {
employeeList.add(employee);
return employee;
}
}
Now its time to run our application and see the output. So go to SpringBootWebApplication class and run as java application.You can see the console that inbuilt tomcat application started on port 8080.
You can change this default port number in the application.properties file shown below.
I am also using servlet context path which is also configured in applcation.properties.
application.properties
server.port=8085
server.servlet.context-path=/employee-service
Hit the below URL in the browser to see the output.
http://localhost:8085/employee-service/employees
You might be wondering how to test the post method of your service. So no worries you can refer to configuring swagger and test application in next post.
Code from Github
Configuring Swagger to test rest api
Give you suggestion in comments.If you like the post Share with your friends on social network.
Creating restful web services using Spring Boot in 15 minutes
Reviewed by JavaInstance
on
4:28:00 AM
Rating:
Reviewed by JavaInstance
on
4:28:00 AM
Rating:




No comments: