I'm currently following a course on Spring Boot and making a Reddit clone. https://www.youtube.com/watch?v=DKlTBBuc32c
During the course, we have a service that uses Mapstruc to map a DTO.
My issue is that when I try to inject the mapper into the service, Spring cannot find the class and gives error:
Parameter 1 of constructor in com.subreddit.service.SubredditService required a bean of type 'com.subreddit.mapper.SubredditMapper' that could not be found.
Consider defining a bean of type 'com.subreddit.mapper.SubredditMapper' in your configuration.
Here is the service:
package com.subreddit.service;
import static java.util.stream.Collectors.toList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.subreddit.dto.SubredditDto;
import com.subreddit.exceptions.SpringRedditException;
import com.subreddit.mapper.SubredditMapper;
import com.subreddit.model.Subreddit;
import com.subreddit.repository.SubredditRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Service
@AllArgsConstructor
@Slf4j
public class SubredditService {
private final SubredditRepository subredditRepository;
private SubredditMapper subredditMapper;
@Transactional
public SubredditDto save(SubredditDto subredditDto) {
Subreddit save = subredditRepository.save(subredditMapper.mapDtoToSubreddit(subredditDto));
subredditDto.setId(save.getId());
return subredditDto;
}
@Transactional(readOnly = true)
public List<SubredditDto> getAll() {
return subredditRepository.findAll()
.stream()
.map(subredditMapper::mapSubredditToDto)
.collect(toList());
}
public SubredditDto getSubreddit(Long id) {
Subreddit subreddit = subredditRepository.findById(id)
.orElseThrow(() -> new SpringRedditException("No subreddit found with ID - " + id));
return subredditMapper.mapSubredditToDto(subreddit);
}
}
Here is the mapper:
package com.subreddit.mapper;
import java.util.List;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import com.subreddit.dto.SubredditDto;
import com.subreddit.model.Post;
import com.subreddit.model.Subreddit;
import lombok.RequiredArgsConstructor;
@Mapper(componentModel = "spring")
public interface SubredditMapper {
@Mapping(target = "numberOfPosts", expression = "java(mapPosts(subreddit.getPosts()))")
SubredditDto mapSubredditToDto(Subreddit subreddit);
default Integer mapPosts(List<Post> numberOfPosts) {
return numberOfPosts.size();
}
@InheritInverseConfiguration
@Mapping(target = "posts", ignore = true)
Subreddit mapDtoToSubreddit(SubredditDto subredditDto);
}
I saw their code. It's working. You didn't follow the steps well.
Take their code from here. Compare what have you done wrong.
Possible issue : The problem is with the package name. @ComponentScan is not able to register the bean from that package as you are unknowingly sending wrong package name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With