javaspringspring-bootmapstruct

Adding a new package broke my Spring Boot Application


I'm getting troubles since I have added a new package in my spring boot application containing Mapstructs' mappers. Looks like the field:

@Autowired    
private UtenteMapper utenteMapper; 

is not being populated in my Service component. The Spring boot main class, PaniECumpanagghiuApplicationConfiguration, is quite essential, (it contains only the @SpringBootApplicationAnnotation annotation), but I've tried previously to add a @ComponentScan.

@SpringBootApplication    
@ComponentScan("PaniECumpanagghiu.PaniECumpanagghiu.Mappers")

In this way the functionality is being recovered, but the @Controller is not fired (when I invoke my API the server retrieves 404). I tried also to add all the packages into the annotation @ComponentScan, like this

@ComponentScan("PaniECumpanagghiu.PaniECumpanagghiu.Mappers","PaniECumpanagghiu.PaniECumpanagghiu.controller",...)

In this way neither the former object, UtenteMapper, is populated anymore (same behavior than the case without @ComponentScan annotation).

If I comment all references to the new package, and I simplify the API in order to return only a String ("Ciao"), the controller is fired. This is my tree directory

Work tree

I have described everything in the previous section.

EDIT 1 Hey guys i have the impression that the problem is in the mapstruct dependence importing. So I decided to give you the whole pom.

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://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.3.2-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>PaniECumpanagghiu</groupId>
    <artifactId>PaniECumpanagghiu</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>PaniECumpanagghiu</name>
    <description>Web app di food delivery per la sicilia sud orientale</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>22</java.version>
        <org.mapstruct.version>1.6.2</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-security</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-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <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>
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

</project>

Solution

  • Because you are telling that perhaps the issue is connected to the interaction of Lombok and Mapstruct, perhaps (the pom.xml file says other annotation processors and I don't know if there are really others), you have to include both annotation processors in the maven-compiler-plugin, like this:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <annotationProcessorPaths>
            <path>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
              <version>${lombok.version}</version>
            </path>
            <path>
              <groupId>org.mapstruct</groupId>
              <artifactId>mapstruct-processor</artifactId>
              <version>${org.mapstruct.version}</version>
            </path>
          </annotationProcessorPaths>
        </configuration>
      </plugin>
    

    Moreover: check if UtenteMapper is annotated with componentModel = "spring", like this:

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

    Without that attribute set (read the javadoc) the generated mapper can't be managed as a Spring bean and won't be injected with @Autowired.

    As a side note, just for conventions' sake: please use lower case starting packages, I suppose Eclipse is telling this already as a warning when creating classes and other packages.