-
Notifications
You must be signed in to change notification settings - Fork 245
8. Build Create Employee REST API
Ramesh Fadatare edited this page Sep 11, 2022
·
2 revisions
package net.javaguides.springboot.service;
import net.javaguides.springboot.entity.Employee;
public interface EmployeeService {
Employee createEmployee(Employee employee);
}
package net.javaguides.springboot.service.impl;
import lombok.AllArgsConstructor;
import net.javaguides.springboot.entity.Employee;
import net.javaguides.springboot.repository.EmployeeRepository;
import net.javaguides.springboot.service.EmployeeService;
import org.springframework.stereotype.Service;
@Service
@AllArgsConstructor
public class EmployeeServiceImpl implements EmployeeService {
private EmployeeRepository employeeRepository;
@Override
public Employee createEmployee(Employee employee) {
return employeeRepository.save(employee);
}
}
package net.javaguides.springboot.controller;
import lombok.AllArgsConstructor;
import net.javaguides.springboot.entity.Employee;
import net.javaguides.springboot.service.EmployeeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@AllArgsConstructor
@RequestMapping("api/employees")
public class EmployeeController {
private EmployeeService employeeService;
@PostMapping
public ResponseEntity<Employee> createEmployee(@RequestBody Employee employee){
Employee savedEmployee = employeeService.createEmployee(employee);
return new ResponseEntity<>(savedEmployee, HttpStatus.CREATED);
}
}
URL: http://localhost:8080/api/employees
Request:
{
"firstName": "Ramesh",
"lastName":"Fadatare",
"email":"ramesh@gmail.com"
}