0

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.

Somebody please help me. enter image description here

//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]
Kumar
  • 13
  • 2
  • I checked the other post similar to this and still didn't find the answer to it. https://stackoverflow.com/questions/55049763/spring-boot-rest-api-gives-empty-rows – Kumar Dec 03 '21 at 11:45
  • Often, a good place to start looking is application log files. In fact, if you switch on logging of SQL statements, you can see any attempt to interact with the database. You'll also see if you have any Spring configuration errors or any other errors that might occur. – Matt Vickery Dec 03 '21 at 11:49
  • @MattVickery I dont know where to see Application log files. But I'm not sure if this helps, in the Employee class, when I give ''' @Column(name = "first_name", nullable = false) ''' I get the 50x error in Postman. Even though I give the data in the POST statement, it is considering it as an empty one. – Kumar Dec 03 '21 at 11:54
  • I see. I would consider (in the strongest possible terms) setting up your logging so that you can see what's happening on application startup and execution. Try following this guide: https://www.tutorialspoint.com/spring/logging_with_log4j.htm you won't need to add any new log statements, you just need to see what your application components (Spring Framework etc) have to say. Set your logging at DEBUG level. – Matt Vickery Dec 03 '21 at 12:01
  • @FaeemazazBhanej I thought the new version Spring Boot automatically calls the ' Autowired. – Kumar Dec 03 '21 at 12:07
  • Thanks @MattVickery. While I do that, can you please guide me where I'm going wrong in this? – Kumar Dec 03 '21 at 12:09
  • The logs will tell you everything you need to know, it looks like you're getting an internal server error. How have you wired your repository into your service class, through a Java @Configuration class? – Matt Vickery Dec 03 '21 at 12:18

0 Answers0