Thursday 15 August 2019

Spring Boot RESTful Web Service Complete Example

While exploring spring boot, I have decided to implement simple REST service to demonstrate GET, POST, PUT, and DELETE calls.

Prerequisite for this project :

- Eclipse with STS (Spring tool Suite) plugin
- SOAPUI
- JAVA 8
- Maven

1. Create Eclipse project.
Select File->New-> Project->Spring Starter Project
Follow the wizards and finally click on finish




2. Define Employee bean.

public class Employee {
private String firstName;
private String lastName;
private int age;
private int employeeID;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getEmployeeID() {
return employeeID;
}
public void setEmployeeID(int employeeID) {
this.employeeID = employeeID;
}

}

3. Create REST Controllers for (GET, POST, PUT, and DELETE) method. This can be achieved with single controllers.





4. Run project from Eclipse from Boot dashboard.

5. Create SOAP UI REST projects to test project.Please find below sample output screens.





Note : Along with above mentioned classes, I have also added EmployeeOprationResponse and EmployeeRegistration classes. You can have a look into source code for more details.Don't forget to enable component scanning in spring boot configuration class (SpringBootRestApplication in this project).  Port is defined in /spring-boot-rest/src/main/resources/application.properties.
Source Code Available here : https://github.com/jagdevsingh87/spring-boot-rest.git

2 comments: