I'm new to REST APIs and Spring Boot. I have been trying to create the REST APIs using STS and MySQL that stores Employees data.
Everything seems fine but when I POST the data in Postman, empty rows are storing in the MySQL and body.
//My employee class in the model
package com.example.SpringBootApplicationProject.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
//We need to make this simple Employee class to JPA annotation
@Data
@Entity
@Table(name="employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
@Column(name = "salary")
private long salary;
}
//Employee Controller Class
package com.example.SpringBootApplicationProject.controller;
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;
import com.example.SpringBootApplicationProject.model.Employee;
import com.example.SpringBootApplicationProject.service.EmployeeService;
@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
private EmployeeService employeeService;
public EmployeeController(EmployeeService employeeService) {
super();
this.employeeService = employeeService;
}
//Build employee REST API
@PostMapping
public ResponseEntity<Employee> saveEmployee(@RequestBody Employee employee){
return new ResponseEntity<Employee>(employeeService.saveEmployee(employee), HttpStatus.CREATED);
}
}
//Employee Repository class
package com.example.SpringBootApplicationProject.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.SpringBootApplicationProject.model.Employee;
//This is an interface.
//JPA repository automatically provides @Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
//public interface EmployeeRepository extends JpaRepository<Employee, Long>{
//Employee save(Employee employee);
}
//Employee Service Class
package com.example.SpringBootApplicationProject.service;
import com.example.SpringBootApplicationProject.model.Employee;
public interface EmployeeService
{
Employee saveEmployee(Employee employee);
}
//Employee Service Impl
package com.example.SpringBootApplicationProject.service.impl;
import org.springframework.stereotype.Service;
import com.example.SpringBootApplicationProject.model.Employee;
import com.example.SpringBootApplicationProject.repository.EmployeeRepository;
import com.example.SpringBootApplicationProject.service.EmployeeService;
@Service
public class EmployeeServiceImpl implements EmployeeService{
private EmployeeRepository employeeRepository;
public EmployeeServiceImpl(EmployeeRepository employeeRepository) {
super();
this.employeeRepository = employeeRepository;
}
@Override
public Employee saveEmployee(Employee employee) {
return employeeRepository.save(employee);
}
}
Postman blank data[MySQL Empty DB][1]