1

I'm working in spring boot with Mappstruct and Lombok. When I use the Mapper anotation to auto implement the mapper class, the anotation is not generating correcty the mapper implementation for my classes.

This is 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>2.3.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.domotichouse.app</groupId>
<artifactId>SpringDomoticHouse</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringDomoticHouse</name>
<description>Spring proyect for the domotic of my house</description>

<properties>
    <java.version>11</java.version>
    <mapstruct.version>1.3.1.Final</mapstruct.version>
    <lombok.version>1.18.16</lombok.version>
    <log4j2.version>2.13.3</log4j2.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-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-to-slf4j</artifactId>
        <version>${log4j2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-jdk8</artifactId>
        <version>${mapstruct.version}</version>
    </dependency>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>${mapstruct.version}</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <source>11</source>
                <target>11</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${mapstruct.version}</version>
                    </path>
                    <annotationProcessorPath>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${lombok.version}</version>
                    </annotationProcessorPath>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

This my 2 objects to map one to other: 1st one:

 import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Data;
 import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@SuppressWarnings("unused")
public class User {

    private Integer id;

    private String usrLogin;

    private String pass;

    private String name;

    private String surname;

    private Boolean enabled;

}

2nd one:

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@SuppressWarnings("unused")
public class UserRequest {

    private Integer id;

    private String usrLogin;

    private String pass;

    private String name;

    private String surname;

    private Boolean enabled;

}

My Mapper inteface:

import com.domotichouse.app.SpringDomoticHouse.application.dto.User;
import com.domotichouse.app.SpringDomoticHouse.rest.dto.UserRequest;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;


@Mapper
public interface UserRestMapper {
    
    UserRestMapper MAPPER = Mappers.getMapper(UserRestMapper.class);
    
    User toAplication(UserRequest request);
    
}

And here is how the anotation implements the interface:

import com.domotichouse.app.SpringDomoticHouse.application.dto.User;
import com.domotichouse.app.SpringDomoticHouse.rest.dto.UserRequest;

public class UserRestMapperImpl implements UserRestMapper {
    public UserRestMapperImpl() {
    }

    public User toAplication(UserRequest request) {
        if (request == null) {
            return null;
        } else {
            User user = new User();
            return user;
        }
    }
}

As you can see the Mapper dont map anything. PD: I have the lombok pluging installed and the @Data and @Builder anotations work as expected

ProRiderZ115
  • 115
  • 2
  • 9

3 Answers3

0

We have it working like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven-compiler-plugin.version}</version>
    <configuration>
        <source>11</source>
        <target>11</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>${mapstruct.version}</version>
            </path>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </path>
        </annotationProcessorPaths>
        <compilerArgs>
            <compilerArg>
                -Amapstruct.defaultComponentModel=spring
            </compilerArg>
        </compilerArgs>
    </configuration>
</plugin>

Note the difference in the <annotationProcessorPaths/> section.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
  • Try removing optional and setting the lombok scope to provided. – Mike Thomsen Nov 08 '20 at 00:00
  • Not working too... I did mvn clean and install and the classes are generated the same as berfore... – ProRiderZ115 Nov 08 '20 at 09:28
  • I did clean install and nothing works. I run the application and the classes have been generated correctly. I stoped the app and did again clean intall. I run it again and the classes are not correct again... – ProRiderZ115 Nov 08 '20 at 10:16
0

Your question seems to be a duplicate of this one: MapStruct and Lombok not working together

Instead of

<annotationProcessorPath>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>${lombok.version}</version>
</annotationProcessorPath>

you have to use

<path>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>${lombok.version}</version>
</path>

Maven dependency "mapstruct-processor" should be provided, like this:

<scope>provided</scope>

After this you must run Maven build with package goal (mvn package), then the generated class UserRestMapperImpl should be as you need it.

Elmar Brauch
  • 1,286
  • 6
  • 20
0
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <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>${lombok.version}</version>
                        </path>
                        <dependency>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok-mapstruct-binding</artifactId>
                            <version>0.2.0</version>
                        </dependency>
                    </annotationProcessorPaths>
                    <compilerArgs>
                        <compilerArg>
                            -Amapstruct.defaultComponentModel=spring
                        </compilerArg>
                    </compilerArgs>
                </configuration>
            </plugin>
vishal palla
  • 41
  • 1
  • 8