У меня есть следующий картограф:
@Mapper
@Component
public interface PriceEntityMapper {
@Mapping(source = "brandId", target = "brandId")
@Mapping(source = "startDate", target = "startDate", qualifiedByName = "timestampToLocalDateTime")
@Mapping(source = "endDate", target = "endDate", qualifiedByName = "timestampToLocalDateTime")
@Mapping(source = "priceList", target = "priceList")
@Mapping(source = "productId", target = "productId")
@Mapping(source = "priority", target = "priority")
@Mapping(source = "price", target = "price")
@Mapping(source = "curr", target = "currency")
Price priceEntityToPrice(PriceEntity priceEntity);
@Named("timestampToLocalDateTime")
default LocalDateTime timestampToLocalDateTime(Timestamp timestamp) {
return timestamp.toLocalDateTime();
}
}
и следующая реализация:
@Service
public class PriceServiceImpl implements PriceService {
private final PriceRepository priceRepository;
private final PriceEntityMapper priceEntityMapper;
@Autowired
public PriceServiceImpl(PriceRepository priceRepository, PriceEntityMapper priceEntityMapper) {
this.priceRepository = priceRepository;
this.priceEntityMapper = priceEntityMapper;
}
@Override
public Price getPrice(LocalDateTime applicationDate, Integer productId, Integer brandId) {
try {
//PriceEntity priceEntity = priceRepository.findByBrandIdAndProductIdAndStartDateLessThanEqualApplicationDateAndEndDateGreaterThanEqualApplicationDate(brandId, productId, Timestamp.valueOf(applicationDate));
return priceEntityMapper.priceEntityToPrice(new PriceEntity());
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Price not found", e);
}
}
}
ну, когда я пытаюсь запустить приложение, выдает следующую ошибку:
Параметр 1 конструктора в com.dharian.application.service.PriceServiceImpl требовал bean-компонента типа com.dharian.infraestructure.mapper.PriceEntityMapper, который не удалось найти.
Но у меня есть аннотации @Mapper и @Component, а еще у меня есть
@SpringBootApplication
@ComponentScan({"com.dharian.infraestructure", "com.dharian.application"})
public class PruebatecnicaApplication {
public static void main(String[] args) {
SpringApplication.run(PruebatecnicaApplication.class, args);
}
}
Для сканирования посылок. Это не работает.
что это может быть?
Я попробовал @ComponentScan, изменил автоподключение и аннотацию к классу Mapper.




Вместо использования @Component вам нужно объявить его как bean-компонент способом MapStruct:
@Mapper(componentModel = "spring")
public interface PriceEntityMapper {
Только теперь инициализируйте его с помощью @Autowired, и он будет создан как компонент.
В противном случае вам придется объявить картограф напрямую:
PriceEntityMapper mapper = Mappers.getMapper(PriceEntityMapper.class)
Для получения дополнительной информации см. документацию для раздела Параметры конфигурации MapStruct -> mapstruct.defaultComponentModel:
Я пробовал, но мне говорят, что у меня недостаточно репутации.
действительно это работает как минимум с 15
@Дхариан, ты также можешь проголосовать здесь, это поможет другим участникам понять, что такой ответ работает.