maciejka
maciejka

Reputation: 948

Cannot inject mapstruct interface during startup spring application

I have the following mapper (mapstruct version 1.3.1.Final).

@Mapper(componentModel = "spring", uses = {}, unmappedTargetPolicy = ReportingPolicy.WARN)
public interface AccountMapper {
    @Mapping(source = "registrationDto.email", target = "email")
    @Mapping(source = "passwordDto.hashPassword", target = "password")
    Account from(RegistrationDto registrationDto, PasswordDto passwordDto);
}

When I attempt to run spring application I got the problem that bean associated with Mapper is not found.

Parameter 1 of constructor in com.xx.xx.Controller required a bean of type 'com.xxx.AccountMapper' that could not be found.

Consider defining a bean of type 'com.xxx.AccountMapper' in your configuration.

I tried solution with decorator. By adding annotation @DecoratedWith(AccountMapperDecorator.class) for interface and creating the following class.

@Component
public abstract class AccountMapperDecorator implements AccountMapper {

    @Autowired
    @Qualifier("delegate")
    private AccountMapper delegate;

    @Override
    public Account from(RegistrationDto registrationDto, PasswordDto passwordDto) {
        return delegate.from(registrationDto, passwordDto);
    }
}

And then I receive.

No qualifying bean of type 'com.xxx.AccountMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value="delegate")}

What could be the problem in that case ?

Upvotes: 1

Views: 1507

Answers (2)

Daniel Taub
Daniel Taub

Reputation: 5359

This is a situation that happens from time to time,
If you will noticed then probebly the AccountMapperImpl isn't generated to the target/build folders There a few ways to fix that:

  • Try clean the out/build directories
  • Run mvn clean compile or gradle clean compileJava
  • Set your IDE to run your code with maven/gradle enter image description here
  • Be sure that Annotation Processing is enabled

Upvotes: 0

Pistolnikus
Pistolnikus

Reputation: 366

Did you run mvn package? When you search for class AccountMapperImpl in your IDE, can you find it? If not that is a problem. If you can't find it, Spring won't too.

Maybe you forgot to configure (or misconfigured) mapstruct-processor in your pom.xml? Do you have something like:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>...</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

there?

Upvotes: 2

Related Questions