In this post, we will look at how to integrate Spring Boot with external systems using REST. We will use a simple example to illustrate how this can be done.
REST is an acronym for Representational State Transfer. It is a software architectural style that defines a set of constraints to be used for creating web services. Web services that conform to the REST architectural style are called RESTful web services.
RESTful web services are easy to build and they are well suited for supporting scalable, distributed systems. RESTful web services can be built on any platform and they can be consumed by any client that supports the HTTP protocol.
There are two ways to create a RESTful web service:
In this post, we will focus on how to create a RESTful web service using Spring Boot.
Spring Boot is a framework that makes it easy to create stand-alone, production-grade Spring-based applications. It provides a opinionated approach to configuring and building Spring-based applications.
Spring Boot makes it easy to create stand-alone, production-grade Spring-based applications. It provides a wide range of features that can be used to build, test, and deploy Spring-based applications.
We will use the following tools and technologies to create our Spring Boot-based RESTful web service:
We will use Spring Initializr to generate our Spring Boot project. We will use the following settings:
We will use H2 as our database. We will configure H2 in the application.properties file:
spring.h2.console.enabled=true
spring.h2.console.path=/h2
We will create a simple entity called Employee:
@Entity
public class Employee {
@Id
@GeneratedValue
private Long id;
private String name;
private String role;
// Getters and setters
}
We will create a simple JpaRepository interface for our Employee entity:
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
We will create a simple EmployeeService interface:
public interface EmployeeService {
Employee save(Employee employee);
List<Employee> findAll();
Employee findOne(Long id);
void delete(Long id);
}
We will create a simple EmployeeServiceImpl class:
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
@Override
public Employee save(Employee employee) {
return employeeRepository.save(employee);
}
@Override
public List<Employee> findAll() {
return employeeRepository.findAll();
}
@Override
public Employee findOne(Long id) {
return employeeRepository.findOne(id);
}
@Override
public void delete(Long id) {
employeeRepository.delete(id);
}
}
We will create a simple EmployeeController class:
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@RequestMapping(method = RequestMethod.GET)
public List<Employee> findAll() {
return employeeService.findAll();
}
@RequestMapping(method = RequestMethod.POST)
public Employee save(@RequestBody Employee employee) {
return employeeService.save(employee);
}
@RequestMapping(method = RequestMethod.GET, value = "/{id}")
public Employee findOne(@PathVariable Long id) {
return employeeService.findOne(id);
}
@RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
public void delete(@PathVariable Long id) {
employeeService.delete(id);
}
}
We will use the following curl commands to test our EmployeeController:
To save an employee:
curl -X POST -H "Content-Type: application/json" -d '{"name":"John Smith","role":"Developer"}' http://localhost:8080/employees
To find all employees:
curl -X GET http://localhost:8080/employees
To find an employee with id 1:
curl -X GET http://localhost:8080/employees/1
To delete an employee with id 1:
curl -X DELETE http://localhost:8080/employees/1
In this post, we looked at how to create a RESTful web service using Spring Boot. We saw how to create the project, configure the project, create the entity, create the repository, create the service, create the service implementation, create the REST controller, and test the REST controller.