0

I'm working on a Spring Boot application and I'm trying to inject a SourcingMapper bean into my SourcingServiceImpl class using the @Autowired annotation, but I keep getting an error when I try to start the application:

Field sourcingMapper in com.immobilia.immobiliaapp.service.SourcingServiceImpl required a bean of type 'com.immobilia.immobiliaapp.mapper.SourcingMapper' that could not be found.

My MapStruct Interface:

@Mapper(componentModel = "spring")
@Component
public interface SourcingMapper {
    SourcingDto toSourcingDto(SourcingController sourcing);
    SourcingController toSourcing(SourcingDto sourcingDto);

    List<SourcingDto> toSourcingDto(List<SourcingController> sourcings);

    List<SourcingController> toSourcing(List<SourcingDto> sourcingDtos);
}

My Service:

@Service
public class SourcingServiceImpl implements SourcingService {

    @Autowired
    SourcingRepo sourcingRepo;

    @Autowired
    SourcingMapper sourcingMapper;
    @Override
    public SourcingDto addSourcing(SourcingDto sourcing) {
        return sourcingRepo.save(sourcing);
    }
}


My pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.immobilia</groupId>
    <artifactId>immobilia-app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>immobilia-app</name>
    <description>immobilia-app</description>
    <properties>
        <java.version>19</java.version>
        <org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>

                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
Phil Ku
  • 191
  • 1
  • 6
  • You need to add the mapstruct annotation processor to your Maven file https://mapstruct.org/documentation/installation/ – daniu Mar 16 '23 at 22:08
  • still have the problem. – ÄLi D Yëss Mar 16 '23 at 22:40
  • @ÄLiDYëss can you provide the codes of the classes `SourcingController` and `SourcingDto`? – Phil Ku Mar 17 '23 at 02:37
  • before all `@Component` on mapper is not necessary. after that, follow this guide (https://examples.javacodegeeks.com/spring-boot-mapstruct-example/) and adjust your pom.xml because often is just a matter of configuration – Luca Basso Ricci Mar 17 '23 at 07:05

2 Answers2

0

I think you are missing lombok-mapstruct-binding in pom.xml file, please refer here

fixcer
  • 129
  • 1
  • 5
0

Thanks, I fixed the issue by adding some attributes to my pom.xml file:


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.immobilia</groupId>
    <artifactId>immobilia-app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>immobilia-app</name>
    <description>immobilia-app</description>
    <properties>
        <java.version>19</java.version>
        <org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.18.20</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok-mapstruct-binding</artifactId>
                            <version>0.2.0</version>
                        </path>

                    </annotationProcessorPaths>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>