Spring Boot Mapstruct返回类型

mgdq6dx1  于 11个月前  发布在  Spring
关注(0)|答案(2)|浏览(171)

没有为返回类型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);
}

字符串
我的服务...

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


有人能帮帮我吗?

dw1jzc5e

dw1jzc5e1#

这是MapStruct中的一个已知问题。看看mapstruct#607
对此有一个解决方案(我认为是由于bug)。检查仅在第一个源参数和结果类型之间进行。但是您需要一个 Package 器类型,以便能够使用@Mapping和多个参数。这意味着以下内容将工作:😄

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);

}

字符串
检查将在Reflector和Wrapper之间进行,并且它将被允许。为了不暴露虚拟对象,您可以执行以下操作:

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);

}


同样,这是一个解决方案,使MapStruct工作,它不是一个功能。按照链接的问题,以了解官方支持何时会来。

ujv3wf0j

ujv3wf0j2#

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

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

字符串
像这样的?

相关问题