EXPERIMENT:15 Date:
Demonstrate how to create a RESTful service.
Step 1: Create a Spring Boot Project:-
Use Spring Initializr ([Link] to generate a new Spring Boot project. Now fill
all the details such as:
Project = maven
Language = java
Spring boot = 3.3.4
Group = [Link]
Artifacts = employee
Packaging = jar
Select the following dependencies:
- Spring Web
- Spring Data JPA
- H2 Database
Click on Generate and extract the zip file on your desktop.
Step 2: Now open the extracted file in VsCode.
Step 3: Install the following Extensions in Vs Code:
Java Extension Pack (provides full Java language support): Search for and install the
Java Extension Pack in VS Code's extension marketplace.
Spring Boot Extension Pack: Install Spring Boot Extension Pack, which includes
tools for running and debugging Spring Boot projects.
Maven Extension: Install Maven for Java extensions for managing your
dependencies.
Step 4: Then create model, repository, service and controller folder in
src/main/java/com/example/employee.
Step 5: In Model folder, create an [Link] file and add the following code:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = [Link])
private Long id;
private String name;
private String department;
private double salary;
// Constructors, Getters and Setters
public Employee() {}
public Employee(String name, String department, double salary) {
[Link] = name;
[Link] = department;
[Link] = salary;
}
// Getters and setters
public Long getId() {
return id;
}
public void setId(Long id) {
[Link] = id;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
[Link] = department;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
[Link] = salary;
}
}
Step 6: In the Repository folder, create an [Link] file and add the
following code:
package [Link];
import [Link];
import [Link];
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
Step 7: In the Service folder, create a [Link] file and add the following code.
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
public List<Employee> getAllEmployees() {
return [Link]();
}
public Optional<Employee> getEmployeeById(Long id) {
return [Link](id);
}
public Employee addEmployee(Employee employee) {
return [Link](employee);
}
public Employee updateEmployee(Long id, Employee employee) {
if([Link](id)) {
[Link](id);
return [Link](employee);
}
return null;
}
public void deleteEmployee(Long id) {
[Link](id);
}
}
Step 8: In the Controller folder, create a [Link] file and add the following
code:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping
public List<Employee> getAllEmployees() {
return [Link]();
}
@GetMapping("/{id}")
public ResponseEntity<Employee> getEmployeeById(@PathVariable Long id) {
Optional<Employee> employee = [Link](id);
return [Link](ResponseEntity::ok).orElseGet(() ->
[Link]().build());
}
@PostMapping
public Employee addEmployee(@RequestBody Employee employee) {
return [Link](employee);
}
@PutMapping("/{id}")
public ResponseEntity<Employee> updateEmployee(@PathVariable Long id,
@RequestBody Employee employee) {
Employee updatedEmployee = [Link](id, employee);
if(updatedEmployee != null) {
return [Link](updatedEmployee);
} else {
return [Link]().build();
}
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteEmployee(@PathVariable Long id) {
[Link](id);
return [Link]().build();
}
}
Step 9: Now to run the application, Go to SprinBoot Dashboard and click on run. It will run
on the default port `8080`. You can now access the endpoints and test the RESTful API.
Step 10: To test the RESTful API, Use the Postman tool to interact with the RESTful
service:
- Go to menu, select file -> new
- Select http method.
To add New Employee:
- Select POST method
- In URL type [Link]
- Go to body, Select raw.
- Type the following code and send the request
Sample JSON body:
{
"name": "John Doe",
"department": "Engineering",
"salary": 80000
}
To Get Employee Details:
- Select GET method
- In URL type [Link]
To Update the Employee Details :
- Select PUT method
- In URL type [Link]
- Go to body, Select raw.
- Type the following code and send the request
Sample JSON body:
{
"name": "Jane Doe",
"department": "Engineering",
"salary": 95000
}
To Delete Employee :
- Select DELETE method
- In URL type, [Link]