model/Employee.
java
package com.example.employeedetails.model;
public class Employee {
private int id;
private String name;
private String department;
private double salary;
public Employee() {
}
public Employee(int id, String name, String department, double salary) {
this.id = id;
this.name = name;
this.department = department;
this.salary = salary;
}
// Getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
service/EmployeeService.java
import java.util.ArrayList;
import java.util.List;
@Service
public class EmployeeService {
private final List<Employee> employeeList = new ArrayList<>();
public List<Employee> getAllEmployees() {
return employeeList;
}
public void addEmployee(Employee employee) {
employeeList.add(employee);
}
}
controller/EmployeeController.java
import java.util.List;
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping
public List<Employee> getAllEmployees() {
return employeeService.getAllEmployees();
}
@PostMapping
public String addEmployee(@RequestBody Employee employee) {
employeeService.addEmployee(employee);
return "Employee added successfully!";
}
}