Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapstruct return type

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?

like image 862
Felipe A. Avatar asked Sep 21 '26 09:09

Felipe A.


1 Answers

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.

like image 72
Filip Avatar answered Sep 24 '26 11:09

Filip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!