Mapstruct return type

7.2k Views Asked by At

No implementation type is registered for return type org.springframework.data.domain.Page.

@Mapper(componentModel = "spring", uses = { OptionalMapper.class, VehicleImageMapper.class, GearShiftMapper.class,
    FuelMapper.class, ColorMapper.class, ModelMapper.class, UserMapper.class })
public interface VehicleMapper {

    VehicleMapper INSTANCE = Mappers.getMapper(VehicleMapper.class);

    VehicleDTO vehicletoVehicleDTO(Vehicle vehicle);

    Page<VehicleDTO> vehicletoVehicleDTO(Page<Vehicle> vehicles);

    Iterable<VehicleDTO> vehicletoVehicleDTO(Iterable<Vehicle> vehicles);

    Vehicle vehicleDTOtoVehicle(VehicleDTO vehicleDTO);
}

My service...

@Override
public Page<VehicleDTO> searchVehiclesByPage(Pageable page) {
    Page<VehicleDTO> vehicles = vehicleMapper.vehicletoPageVehicleDTO(vehicleRepository.findAllByEnabled(page));
    return vehicles;
}

Can someone help me plz?

2

There are 2 best solutions below

0
On

This is a known issue in MapStruct. Have a look at mapstruct#607.

There is a workaround for this (I think due to a bug ). The check is only done between the first source parameter and the result type. You will need a wrapper type though, in order to be able to use @Mapping and multiple parameters. Which means that the following will work:

public class Wrapper<T> {
    private T value;
    //getters and setters
}

public interface MyMapper {

    @Mapping(source = "customers", target = "value")
    Wrapper<PageDTO<VehicleDTO>> map(Integer dummy, Page<Vehicle> vehicles);

}

The check will be done between Integer and Wrapper and it'll be allowed. In order not to expose the dummy you can do something like:

public abstract class MyMapper {

    public PageDTO<VehicleDTO> map(Page<Vehicle> vehicles) {
        return map(1, vehicles).getValue(); //Maybe do null checks as well
    }

    @Mapping(source = "customers", target = "value")
    protected Wrapper<PageDTO<VehicleDTO>> map(Integer dummy, Page<Vehicle> vehicles);

}

Again this is a workaround to make MapStruct work and it is not a feature. Follow the linked issue in order to know when official support will come.

1
On
@Mapper
public interface VehicleMapper {
  VehicleDTO map(Vehicle data);

  default Page<VehicleDTO> map(Page<Vehicle> data) {
    return data.map(this::map);
  }
}

Something like this maybe?