Search code examples
javamapstructmapper

@Mapper annotation not being implemented at compile: missing bean from mapper class


I am having an issue with my mapper interface which creates an exception at compile time : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookServiceImpl' defined in file [/home/emmanuel/Documents/Dev/spring app/hymnbook/hymnbook/target/classes/com/zanah/hymnbook/service/BookServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 1: No qualifying bean of type 'com.zanah.hymnbook.mapper.BookMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Parameter 1 of constructor in com.zanah.hymnbook.service.BookServiceImpl required a bean of type 'com.zanah.hymnbook.mapper.BookMapper' that could not be found.

Mapper Interface


@Mapper(componentModel = "spring")

public interface BookMapper {

    Book bookDtoToBook(BookDto bookDto);
    BookDto bookToBookDto(Book book);

    List<Book> BookDtoToBook(List<BookDto> bookDtoList);
    List<BookDto> bookToBookDto(List<Book> bookList);

    BookDto bookRequestDtoToBookDto(BookRequestDto bookRequestDto);

}

Service Implemenation class

@Service
@AllArgsConstructor
public class BookServiceImpl implements BookService{

@Autowired
    private final BookRepository bookRepository;


@Autowired
    private final BookMapper bookMapper;


    @Override
    public BookDto saveBook(BookDto bookDto) {
        Book recordFromDB = bookRepository.findByName(bookDto.getName());

        if (recordFromDB != null) {
            throw new ResourceAlreadyExistException("Book", "ID", bookDto.getName());
        }

        Book recordToBeSaved = bookMapper.bookDtoToBook(bookDto);

        Book savedRecord = bookRepository.save(recordToBeSaved);

        BookDto result = bookMapper.bookToBookDto(savedRecord);

        return result;

    }

    @Override
    public List<BookDto> findAllBooks() {
        return null;
    }

    @Override
    public BookDto getBookById(Long id) {
        return null;
    }

    @Override
    public BookDto updateBook(BookDto Bookdto, Long id) {
        return null;
    }

    @Override
    public void deleteBook(Long id) {

    }
}

Main Application

@SpringBootApplication
@ComponentScan(basePackages = "com.zanah.hymnbook")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

I have verified my dependancies in pom.xml file to include mapstruct


Solution

  • This helped me solve my problem https://stackoverflow.com/a/50204696/22585216 I had to update to the later version to get it to work 3.11.0