``public List<ProductResponseDTO> getallproducts() {
List<Products> productsList= productRepository.findAll();
List<ProductResponseDTO> productResponseDTOS = new ArrayList<>();
for(Products p: productsList){
ProductResponseDTO productResponseDTO = new ProductResponseDTO();
productResponseDTO.setUuid(p.getUuid());
productResponseDTO.setName(p.getName());
productResponseDTO.setPrice(p.getPrice());
productResponseDTO.setImage(p.getImage());
productResponseDTO.setDescription(p.getDescription());
productResponseDTO.setCategory(p.getCategory());
productResponseDTOS.add(productResponseDTO);
}
return productResponseDTOS`;
Iam having category class inside product class, when trying to use for loop and map it response DTO iam getting many outputs when i try to test the API like below.. is this way of mapping correct
–>productResponseDTO.setCategory(p.getCategory());
[{“uuid”:”30145429-3853-49ad-82af-18b8ba623c68″,”name”:”Go pro Camera”,”price”:10000.0,”category”:{“uuid”:”8a9c09e7-47ac-4b88-b6fa-5acc688c468e”,”name”:”Electronics”,”products”:[{“uuid”:”30145429-3853-49ad-82af-18b8ba623c68″,”name”:”Go pro Camera”,”price”:10000.0,”category”:{“uuid”:”8a9c09e7-47ac-4b88-b6fa-5acc688c468e”,”name”:”Electronics”,”products”:[{“uuid”:”30145429-3853-49ad-82af-18b8ba623c68″,”name”:”Go pro Camera”,”price”:10000.0,”category”:{“uuid”:”8a9c09e7-47ac-4b88-b6fa-5acc688c468e”,”name”:”Electronics”,”products”:[{“uuid”:”30145429-3853-49ad-82af-18b8ba623c68″,”name”:”Go pro Camera”,”price”:10000.0,”category”:{“uuid”:”8a9c09e7-47ac-4b88-b6fa-5acc688c468e”,”name”:”Electronics”,”products”:[{“uuid”:”30145429-3853-49ad-82af-18b8ba623c68″,”name”:”Go pro Camera”,”price”:10000.0,”category”:{“uuid”:”8a9c09e7-47ac-4b88-b6fa-5acc688c468e”,”name”:”Electronics”,”products”:[{“uuid”:”30145429-3853-49ad-82af-18b8ba623c68″,”name”:”Go pro Camera”,”price”:10000.0,”category”:{“uuid”:”8a9c09e7-47ac-4b88-b6fa-5acc688c468e”,”name”:”Electronics”,”products”:[{“uuid”:”30145429-3853-49ad-82af-18b8ba623c68″,”name”:”Go pro Camera”,”price”:10000.0,”category”:{“uuid”:”8a9c09e7-47ac-4b88-b6fa-5acc688c468e”,”name”:”Electronics”,”products”:[{“uuid”:”30145429-3853-49ad-82af-18b8ba623c68″,”name”:”Go pro Camera”,”price”:10000.0,”category”:{“uuid”:”8a9c09e7-47ac-4b88-b6fa-`
n.m. mukesh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.