Nicoll Mejia
Nicoll Mejia

Reputation: 79

How Inyect dependency by interface mapper

I try to use mapstruct in java Spring to mapper object DTO to Object normal

I try to call the interface mapper since the service, but I have NullPointerException, seems the interface is not injected in service I used autowired and I quit this

Service


@Service
public class FollowService implements IFollowService{

    @Autowired
    IFollowRepository iFollowRepository;

    private IUserMapper iUserMapper;

  @Override
    public UserDTOCount countFollowers(int userId) throws UserIdNotFoundException, UserNotFollowException {
        return iUserMapper.toUserCount(iFollowRepository.getUserById(userId));
    }



Mapper

@Mapper(componentModel = "spring")
public interface IUserMapper {

  @Mappings({
          @Mapping(source = "id" , target = "id"),
          @Mapping(source = "name", target = "name"),
          @Mapping(source = "followers", target = "followers", qualifiedByName = "followers")
  })
  UserDTOCount toUserCount(User user);

Error

processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at com.reto1.demo.Service.FollowService.countFollowers(FollowService.java:54) ~[classes/:na]

I try to debug and I see the iUserMapper is Null, I dont know How call since service

Thanks!

Upvotes: 0

Views: 728

Answers (2)

Nicoll Mejia
Nicoll Mejia

Reputation: 79

After see many questions, I resolve the error this form

-First, add plugin in pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source> <!-- depending on your project -->
                <target>1.8</target> <!-- depending on your project -->
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${org.mapstruct.version}</version>
                    </path>
                    <!-- other annotation processors -->
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

Second I added lombok

 <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.18.20</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>1.4.1.Final</version>
                        </path>

And finally, I add autowired

@Service
public class FollowService implements IFollowService{

    @Autowired
    IFollowRepository iFollowRepository;

    @Autowired
    IUserMapper iUserMapper;

Upvotes: 0

Filip
Filip

Reputation: 21433

The reason why the iUserMapper is null in your FollowService is because you are not injecting your mapper.

You need to add @Autowired in your service.

e.g.

@Service
public class FollowService implements IFollowService{

    @Autowired
    IFollowRepository iFollowRepository;

    @Autowired
    private IUserMapper iUserMapper;

    @Override
    public UserDTOCount countFollowers(int userId) throws UserIdNotFoundException, UserNotFollowException {
        return iUserMapper.toUserCount(iFollowRepository.getUserById(userId));
    }

}

Small remark: One small digression note from me. I would suggest not prefixing the interfaces with I. The IDEs can clearly show what is a class and what is an interface and it is also easier to see them in your tree structure as not everything is under "I"

Upvotes: 1

Related Questions