Rest Call in Spring Boot

1. Create Employee class. The important thing to note is we should have 2 constructors, default and with parameters. 

@Entity
@Table(name = "employee007")
public class Employee {
@Id
String employeeId;
String name;
String address;
String emailId;
       //default constructor
       //constructor with parameters
       //setters & getters
}

2. Create EmployeeRepository interface

public interface EmployeeRepository extends JpaRepository<Employee, String>{}


3. Create EmployeeService interface

public interface EmployeeService {
public List<Employee> getAllEmployees();
public Optional<Employee> getEmployee(String empId);
public Employee getEmployeeFromStoredProcedure(String empId);
public Employee getEmployee(Employee employee);
public void addEmployee(Employee employee);
public void updateEmployee(Employee employee, String empId);
public void deleteEmployee(String employee);
}

4. Create EmployeeServiceImpl class

@Service
public class EmployeeServiceImpl implements EmployeeService{
@Autowired
EmployeeRepository employeeRepository;

@Override
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
}

@Override
public Optional<Employee> getEmployee(String empId) {
return employeeRepository.findById(empId);
}

@Override
public Employee getEmployee(Employee emp) {
return null;
}

@Override
public void addEmployee(Employee emp) {
employeeRepository.save(emp);
}

@Override
public void updateEmployee(Employee emp, String empId) {
Optional<Employee> target = employeeRepository.findById(empId);
if(target.isPresent()) {
target.get().setAddress(emp.getAddress());
target.get().setEmailId(emp.getEmailId());
target.get().setName(emp.getName());
employeeRepository.save(target.get());
}
}

@Override
public void deleteEmployee(String empId) {
employeeRepository.deleteById(empId);
}
}

5. Create EmployeeController class

@RestController
public class EmployeeController {
@Autowired
private EmployeeService employeeService;

@RequestMapping("/employees")
public List<Employee> getAllEmployees() {
return employeeService.getAllEmployees();
}

@RequestMapping("/employees/{id}")
public Employee getEmployee(@PathVariable String id) {
return employeeService.getEmployee(id).get();
}

@RequestMapping(value="/employees/", method=RequestMethod.PUT)
public Employee getEmployee(@RequestBody Employee emp) {
return employeeService.getEmployee(emp);
}

@RequestMapping(value="/employees", method=RequestMethod.POST)
public void addEmployee(@RequestBody Employee employee) {
employeeService.addEmployee(employee);
}

@RequestMapping(value="/employees/{id}", method=RequestMethod.PUT)
public void updateEmployee(@RequestBody Employee emp, @PathVariable String id) {
employeeService.updateEmployee(emp, id);
}

@RequestMapping(value="/employees/{id}", method=RequestMethod.DELETE)
public void deleteEmployee(@PathVariable String id) {
employeeService.deleteEmployee(id);
}
}

6. Our application would also look for import.sql in the classpath and execute it, if found.

In our example, we define import.sql under src/main/resources in order to fill our tables with static data:
insert into employee007 (EMPLOYEE_ID, NAME, ADDRESS, EMAIL_ID) values('1','Raghvendra','Bangalore','raghav@gmail.com');
insert into employee007 (EMPLOYEE_ID, NAME, ADDRESS, EMAIL_ID) values('2','Pushpendra','Delhi','pushp@gmail.com');
insert into employee007 (EMPLOYEE_ID, NAME, ADDRESS, EMAIL_ID) values('3','Rahul','Pune','rahul@gmail.com');
insert into employee007 (EMPLOYEE_ID, NAME, ADDRESS, EMAIL_ID) values('4','Rohit','Allahabad','rohit@gmail.com');
insert into employee007 (EMPLOYEE_ID, NAME, ADDRESS, EMAIL_ID) values('5','Ankit','Koubara','ankit@gmail.com');
insert into employee007 (EMPLOYEE_ID, NAME, ADDRESS, EMAIL_ID) values('6','Aniket','Manikpur','aniket@gmail.com');
insert into employee007 (EMPLOYEE_ID, NAME, ADDRESS, EMAIL_ID) values('7','Mishra','Alaska','mishra@gmail.com');
7. Make the call from browser http://localhost:8080/employees/5
It will print Employee with id 5 if available
Similarally we can use other methods. Don't forget to use proper method GET, PUT, POST, DELETE, and content-type (ex. "application/json" or "application/xml" etc) when calling services.

Comments

Popular posts from this blog

SQL basic interview question

gsutil Vs Storage Transfer Service Vs Transfer Appliance