Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed

Ask Question

I am getting below error while creating a spring maven rest webservice project. I am new with springboot, could someone help me with this. The error is:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value : com.example.spring.backend.model.Employee.firstName; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.example.spring.backend.model.Employee.firstName] with root cause

I created a database using MySQL Workbench and defined a table in it to store employee details. Here is my code:

EmployeeController.java

@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
    private EmployeeService employeeService;
    public EmployeeController(EmployeeService employeeService) {
        super();
        this.employeeService = employeeService;
    // build create employee REST API
    @PostMapping()
    public ResponseEntity<Employee> saveEmployee(@RequestBody Employee employee){
        return new ResponseEntity<Employee>(employeeService.saveEmployee(employee), HttpStatus.CREATED);

Employee.java

@Data
@Entity
@Table(name="employees")
public class Employee {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @Column(name = "first_name", nullable = false)
    private String firstName;
    @Column(name = "last_name")
    private String lastName;
    @Column(name = "email")
    private String email;

EmployeeRepository.java

public interface EmployeeRepository extends JpaRepository<Employee, Long>{

EmployeeService.java

public interface EmployeeService {
    Employee saveEmployee(Employee employee);

EmployeeServiceImpl.java

@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);

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/employee_management_system?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
# Hibernate properties
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
# create, create-drop
spring.jpa.hibernate.ddl-auto=update

While sending a post request using postman, I am getting Internal Server Error 500.

**Input**
    "firstName": "abc",
    "lastName": "abc",
    "email": "abc@gmail.com"
**Result:**
    "timestamp": "2022-10-26T09:48:51.322+00:00",
    "status": 500,
    "error": "Internal Server Error",
    "path": "/api/employees"

I am taking reference from a youtube video but in the video, the code works but not in my case.

Please advise what is wrong in my code. Thanks in advance.

The error says that you are trying to insert row employee where firstname is null. To test this. print the firstName in the controller and see if spring converted your rest data to object or not. Also please post the video link, I feel there is some issue in Employee.java – Godwin Oct 26, 2022 at 11:19 Thanx for the comment, I am providing the GitHub link that is used by the youtube video, I am only trying to do a part of the code and have crossed checked it also. Link: github.com/RameshMF/spring-boot-tutorial-course/tree/main/… Please advice what is wrong. Thanks in advance. – Pranay Mahajan Oct 26, 2022 at 12:05 I see you are using Lombok for your data entity. I feel that might be causing problems during serialisation. Try to use Entity class without Lombok with getter/setters and see if it works. – Syed Ammar Oct 26, 2022 at 13:24 Yes, It worked. Thanks for your help. But I am curious, why did it cause error while using Lombok for the data entity. Could you explain that to me. By the way, thanks a lot for your help. – Pranay Mahajan Oct 26, 2022 at 16:16

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.