javaspring-boothibernatejpabackend

Infinite response through Spring boot's entity/dto


I'm trying to test a controller to store employees and department. But when creating employees with the given payload(post employee): { "name": "malay", "dateOfJoining": "2024-12-18", "isActive": true, "department": { "id": 1 } } It results in infinite recursive response, but only when commenting private List<employeesDto> employees in department dto, it works fine. But what if, when i want to know the list of employees in a department. And also i've used @JsonManagedReference, @JsonBackReference and @JsonIgnore on necessary fields. But none of them worked. So how can i implement this in a way where i also get the list of employees in a department as well no infinite recursive response (for instance creating employee)

deptDto

@Data
@AllArgsConstructor
@NoArgsConstructor
public class departmentDto {
    private Integer id;
    @NonNull
    private String name;
    private List<employeeDto> employees;
    private employeeDto hod;
//    private Integer hod_id;
}

empDto

@Data
@AllArgsConstructor
@NoArgsConstructor
public class employeeDto {
    private Integer id;
    private String name;
    private LocalDate dateOfJoining;
    @JsonProperty("isActive")
    private boolean isActive;
    private departmentDto department;
}

empService

public employeeDto createEmployee (employeeDto empDto) {
//
        employeeEntity empEntity = modelMapper.map(empDto, employeeEntity.class);
//        employeeEntity empEntity = new employeeEntity(empDto.getId(),empDto.getName(),empDto.getDateOfJoining(),empDto.isActive());
        // handling the department relationship
        if (empDto.getDepartment() != null && empDto.getDepartment().getId() != null) {
            departmentEntity deptEntity  = departmentRepository.getReferenceById(empDto.getDepartment().getId());
            empEntity.setDepartment(deptEntity);
        }
        employeeEntity createdEmpEntity = employeeRepository.save(empEntity);
//        ModelMapper modelMapper = new ModelMapper();
        employeeDto createdEmpDto = modelMapper.map(createdEmpEntity, employeeDto.class);
//        employeeDto createdEmpDto = new employeeDto(
//                createdEmpEntity.getId(),
//                createdEmpEntity.getName(),
//                createdEmpEntity.getDateOfJoining(),
//                createdEmpEntity.isActive()
//        );
        return createdEmpDto;
    }

deptEntity

@Entity
//@Table(name="department",schema = "spring_emp_dept")
@Table(name="department")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class departmentEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @Column(nullable = false)
    private String name;

    //relationships
    @OneToMany(mappedBy = "department")
    @JsonManagedReference
    private List<employeeEntity> employees;

    @ManyToOne
    @JoinColumn(name = "hod_id")
    @JsonBackReference
    private employeeEntity hod;
}

empEntity:

@Entity
//@Table(name="employees",schema="spring_emp_dept")
@Table(name="employees")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class employeeEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @Column(nullable = false)
    private String name;
    private LocalDate dateOfJoining;
    private boolean isActive;
    // relationships
    @ManyToOne
    @JoinColumn(name="dept_id")
    @JsonBackReference
    private departmentEntity department;
    @OneToMany(mappedBy = "hod")
    @JsonBackReference
    private List<departmentEntity> hodDept;

}

Solution

  • It will get infinite response because you are returning employeeDto, employeeDto has departmentDto and again departmentDto has employeeDtoList and so on this is going in infinte loop, you have only used JsonIgnore in entities not on dtos.and you are returning dto so You have to use @JsnoIgnore on Dto also.