I'm having a Json error because my DTO's are keeping a circular reference each other.
CompanyDTO:
@Data
public class CompanyDTO {
private Long id;
private String name;
private List<CompanyCountryDTO> companyCountries;
//getters and setters
}
CompanyCountryDTO:
@Data
public class CompanyCountryDTO {
private Long id;
private LocalDateTime createdAt;
private CompanyDTO company;
private CountryDTO country;
//getters and setters
CompanyService implementation convert a list of Company to a list of CompanyDTO:
@Override
public List<CompanyDTO> getAllCompaniesWithCountryDTO() {
List<Company> listCompanies = companyRep.findAll();
return listCompanies.stream().map(this::convertToDto).collect(Collectors.toList());
}
private CompanyDTO convertToDto(Company company) {
CompanyDTO companyWithServiceDTO = modelMapper.map(company, CompanyDTO.class);
return companyWithServiceDTO;
}
I would like to do something like this but using ModelMapper because I have others references that are circular:
listCompanies.stream().forEach(company -> {
if (!ObjectUtils.isEmpty(company.getCompanyCountries())) {
company.getCompanyCountries().forEach(companyCountry -> {
companyCountry.setCompany(null);
});
}
});
How can I remove company reference from CompanyCountryDTO since company also have another list of CompanyCountryDTO?