MapStruct Page to List conversion issues

usmanwalana :

I am new to Spring and MapStruct. I am having issues with conversion from Page to List. I used the MapStruct mapper in the service. I need to fetch all products from DB, then convert to ProductResponse using MapStruct mapper and then return PagedResponse object but the following error appears:

java.lang.ClassCastException: org.springframework.data.domain.PageImpl cannot be cast to java.util.List
    at org.walana.GP.service.ProductService.getAll(ProductService.java:67) ~[classes/:na]
    at org.walana.GP.controller.ProductController.getAll(ProductController.java:40)

ReplaceNumber

@Entity
@Table(name = "replace_numbers")
public class ReplaceNumber extends UserDateAudit
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank
    @Size(max = 20)
    private String partNumber;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "product_id", nullable = false)
    private Product product;

    public ReplaceNumber() {}

    public ReplaceNumber(String partNumber)
    {
        this.partNumber = partNumber;
    }
}

ReplaceNumberResponse

public class ReplaceNumberResponse
{
    private Long id;
    private String partNumber;
}

Product

@Entity
@Table(name = "products", indexes = {@Index(name= "part_number_index", columnList = "part_number", unique = true)})
public class Product extends UserDateAudit
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank
    @Column(name = "part_number", nullable = false)
    @Size(max = 20)
    private String partNumber;

    @NotBlank
    @Size(max = 255)
    private String description;

    @OneToMany(
            mappedBy = "product",
            cascade = CascadeType.ALL,
            fetch = FetchType.EAGER,
            orphanRemoval = true
    )
    @Fetch(FetchMode.SELECT)
    private List<ReplaceNumber> replaceNumbers = new ArrayList<>();

    @ManyToOne
    @JoinColumn(name = "product_manufacturer_id", referencedColumnName = "id")
    private ProductManufacturer manufacturer;

    @ManyToOne
    @JoinColumn(name = "product_model_id", referencedColumnName = "id")
    private ProductModel model;

    @ManyToOne
    @JoinColumn(name = "product_category_id", referencedColumnName = "id")
    private ProductCategory category;

    @Column(name = "cost", nullable = false)
    @DecimalMin(message = "Cost should be greater than 1", value = "1")
    private float cost;

    @Column(name = "price", nullable = false)
    @DecimalMin(message = "Price should be greater than 0", value = "0")
    private float price;
}

ProductResponse

public class ProductResponse
{
    private Long id;
    private String partNumber;
    private String description;
    private List<ReplaceNumberResponse> replaceNumberResponses;
    private ProductManufacturerResponse manufacturer;
    private ProductModelResponse model;
    private ProductCategoryResponse category;
    private float cost;
    private float price;
}

ProductMapper

@Mapper(componentModel = "spring")
public interface ProductMapper
{
    ProductResponse toProductResponse(Product product);
    List<ProductResponse> toProductResponses(List<Product> products);
    Product toProduct(ProductResponse productResponse);
}

PagedResponse

public class PagedResponse<T>
{
    private List<T> content;
    private int page;
    private int size;
    private long totalElements;
    private int totalPages;
    private boolean last;

    public PagedResponse() {

    }

    public PagedResponse(List<T> content, int page, int size, long totalElements, int totalPages, boolean last) {
        this.content = content;
        this.page = page;
        this.size = size;
        this.totalElements = totalElements;
        this.totalPages = totalPages;
        this.last = last;
    }
}

ProductService

@Service
public class ProductService
{
    @Autowired
    ProductRepository productRepository;

    @Autowired
    ProductMapper productMapper;

    public PagedResponse<ProductResponse> getAll(UserPrincipal currentUser, int page, int size)
    {

        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createdAt");
        Page<Product> products = productRepository.findAll(pageable);

        if (products.getNumberOfElements() == 0)
        {
            return new PagedResponse<>(Collections.emptyList(), products.getNumber(),
                    products.getSize(), products.getTotalElements(), products.getTotalPages(), products.isLast());
        }

        List<ProductResponse> productResponses = productMapper.toProductResponses((List<Product>) products);

        return new PagedResponse<>(productResponses, products.getNumber(),
                products.getSize(), products.getTotalElements(), products.getTotalPages(), products.isLast());
    }

}

ProductController

@RestController
@RequestMapping("/api/products")
public class ProductController
{
    @Autowired
    private ProductService productService;

    private static final Logger logger = LoggerFactory.getLogger(ProductController.class);

    @GetMapping
    public PagedResponse<ProductResponse> getAll(@CurrentUser UserPrincipal currentUser,
                                                 @RequestParam(value = "page", defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) int page,
                                                 @RequestParam(value = "size", defaultValue = AppConstants.DEFAULT_PAGE_SIZE) int size)
    {
        return productService.getAll(currentUser, page, size);
    }
}