21

I am writing an MVC REST application with Spring Boot and Hibernate. I decided to do DTO mapping using MAPSTRUCT. It seems that I did everything according to the guide, but an error is issued. What is the problem, I cannot understand. There is very little information on forums and on google.

P.S. At first I thought that the problem was in Lombok, so I removed Lombok and manually assigned getters / setters. Then the problem was not solved. I took both in the Drink class and in the DrinkDTO I prescribed getters / setters. It still didn't help.

Drink:

@Entity
@Table(name = "drink", schema = "public")
public class Drink {

    public Drink() { // Constructor for Hibernate

    }


    // Fields
    //
    private @Id
    @GeneratedValue
    Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "price")
    private float price;

    @Column(name = "about")
    private String about;

    @Column(name = "is_deleted")
    private boolean isDeleted;

    // Relationships
    //
    @ManyToOne
    @JoinColumn(name = "packaging_id")
    private Packaging packaging;

    @ManyToOne
    @JoinColumn(name = "manufacturer_id")
    private Manufacturer manufacturer;

    @ManyToOne
    @JoinColumn(name = "country_id")
    private Countries countries;
}

DrinkDTO:

public class DrinkDTO {

    // Fields
    //
    private String drinkName;

    private float drinkPrice;

    private String drinkAbout;

    private Packaging drinkPackaging;

    private Manufacturer drinkManufacturer;

    private Countries drinkCountries;


    // Getters and Setters
    //
    public String getDrinkName() {
        return drinkName;
    }

    public void setDrinkName(String drinkName) {
        this.drinkName = drinkName;
    }

    public float getDrinkPrice() {
        return drinkPrice;
    }

    public void setDrinkPrice(float drinkPrice) {
        this.drinkPrice = drinkPrice;
    }

    public String getDrinkAbout() {
        return drinkAbout;
    }

    public void setDrinkAbout(String drinkAbout) {
        this.drinkAbout = drinkAbout;
    }

    public Packaging getDrinkPackaging() {
        return drinkPackaging;
    }

    public void setDrinkPackaging(Packaging drinkPackaging) {
        this.drinkPackaging = drinkPackaging;
    }

    public Manufacturer getDrinkManufacturer() {
        return drinkManufacturer;
    }

    public void setDrinkManufacturer(Manufacturer drinkManufacturer) {
        this.drinkManufacturer = drinkManufacturer;
    }

    public Countries getDrinkCountries() {
        return drinkCountries;
    }

    public void setDrinkCountries(Countries drinkCountries) {
        this.drinkCountries = drinkCountries;
    }

    // toSTRING
    @Override
    public String toString() {
        return "DrinkDTO{" +
                "drinkName='" + drinkName + '\'' +
                ", drinkPrice=" + drinkPrice +
                ", drinkAbout='" + drinkAbout + '\'' +
                ", drinkPackaging=" + drinkPackaging +
                ", drinkManufacturer=" + drinkManufacturer +
                ", drinkCountries=" + drinkCountries +
                '}';
    }

CustomerController:

@GetMapping("/drinks")
    List<DrinkDTO> getAllDrinks(){
        return DrinkMapper.INSTANCE.drinksToDrinksDTO(customerService.getAllDrinks());
    }

BUILD.GRADLE

// Mapstruct
    implementation 'org.mapstruct:mapstruct:1.3.1.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.3.1.Final'

DrinkMapper:

@Mapper
public interface DrinkMapper {

    DrinkMapper INSTANCE = Mappers.getMapper(DrinkMapper.class);

    @Mapping(source = "name", target = "drinkName")
    @Mapping(source = "price", target = "drinkPrice")
    @Mapping(source = "about", target = "drinkAbout")
    @Mapping(source = "packaging", target = "drinkPackaging")
    @Mapping(source = "manufacturer", target = "drinkManufacturer")
    @Mapping(source = "countries", target = "drinkCountries")
    DrinkDTO drinkToDrinkDTO(Drink drink);

    @Mapping(source = "drinkName", target = "name")
    @Mapping(source = "drinkPrice", target = "price")
    @Mapping(source = "drinkAbout", target = "about")
    @Mapping(source = "drinkPackaging", target = "packaging")
    @Mapping(source = "manufacturer", target = "drinkManufacturer")
    @Mapping(source = "countries", target = "drinkCountries")
    Drink drinkDTOtoDrink(DrinkDTO drinkDTO);

    @Mapping(source = "name", target = "drinkName")
    @Mapping(source = "price", target = "drinkPrice")
    @Mapping(source = "about", target = "drinkAbout")
    @Mapping(source = "packaging", target = "drinkPackaging")
    @Mapping(source = "manufacturer", target = "drinkManufacturer")
    @Mapping(source = "countries", target = "drinkCountries")
    List<DrinkDTO> drinksToDrinksDTO(List<Drink> drinks);
}

ERRORS: enter image description here

Artur Vartanyan
  • 589
  • 3
  • 10
  • 35

10 Answers10

76

For those, who have the same issue when using mapstruct + lombok:

I had the same issue. The reason was that I've been using Lombok plugin too. There's no need to remove it, but you have to ensure that in pom.xml in <annotationProcessorPaths> Lombok tag is before the Mapstruct one.

Example (part of pom.xml file):

<build>
        <pluginManagement>
            <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>
                        <encoding>${project.build.sourceEncoding}</encoding>
                        <annotationProcessorPaths>
                            <path>
                                <groupId>org.projectlombok</groupId> <!-- IMPORTANT - LOMBOK BEFORE MAPSTRUCT -->
                                <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>
            </plugins>
        </pluginManagement>
    </build>
lrkwz
  • 6,105
  • 3
  • 36
  • 59
Jakub Słowikowski
  • 1,063
  • 1
  • 8
  • 13
  • The ordering did not seem to do the trick for me, but adding this annotation processor as well did: ` org.projectlombok lombok-mapstruct-binding 0.2.0 ` – Paul Nov 16 '21 at 11:00
  • I had a problem with the warning from the original question being shown in Netbeans IDE although I am using the `lombok-mapstruct-binding`. A similar issue appeared when using "Mapping Composition" from Mapstruct. Both issues showed a problem in Netbeans' code editor, but were not stopping the project from being compiled and running without problems. What solved these issues for me was ordering the `` like: 1. Lombok 2. Mapstruct-processor 3. Lombok-mapstruct-binding – Daki May 18 '22 at 13:48
  • I had the same issue, the trick is working for me – ColdFire Sep 26 '22 at 22:06
  • 1
    Thanks for that one. Worked for me as well and save me some headache – luso Nov 06 '22 at 22:55
  • 1
    After lots of searching, this finally helped. Thanks for sharing! – observer Jan 09 '23 at 15:51
7

Just to add to @Jakub Słowikowski answer, the same happens with gradle dependencies. The following order was causing the error:

dependencies {    
...
annotationProcessor("org.mapstruct:mapstruct-processor:${mapstructVersion}")

annotationProcessor("org.projectlombok:lombok:${lombokVersion}")
...
}

hence I was forced to switch the order:

dependencies {    
...
annotationProcessor("org.projectlombok:lombok:${lombokVersion}")
    
annotationProcessor("org.mapstruct:mapstruct-processor:${mapstructVersion}")
...
}
João Matos
  • 6,102
  • 5
  • 41
  • 76
6

The error comes from the fact that you tried to map properties from List<> objects, but those don't exist. Mapstruct is smart enough to generate mappers between lists, provided it knows how to map the elements inside the list.

So you don't need to specify @Mapping annotations on the list-to-list mapping. Mapstruct will use the drinkToDrinkDTO mapping method automatically.

@Mapping(...)
DrinkDTO drinkToDrinkDTO(Drink drink);

List<DrinkDTO> drinksToDrinksDTO(List<Drink> drinks);
GeertPt
  • 16,398
  • 2
  • 37
  • 61
1

In my case, the error did not stem from the declaration order of the annotationProcessors but from a missing Lombok annotation.

I had a mapping for Customer to CustomerDto, but the Customer was missing getters and setters.

CustomerDto toCustomerDto(Customer customer);

With

@Data
public class CustomerDto {
  ...
}

and

public class Customer {
  ...
}

Adding @Data to the Customer class solved the issue for me.

94621
  • 53
  • 4
0

Try add a uses parameter in @Mapper annotation if you also have PackakingMapper, CoutriesMapper ... , like this:

@Mapper(uses = {PackagingMapper.class, CountriesMapper.class, ManufacturerMapper.class})
public interface DrinkMapper{
    ....
}
swider96
  • 76
  • 3
0

just put lombok dependencie as first dependencie in your pom.xml file

then reload maven project

you don't need to add mapstruct plugin, i just used dependencies

    <dependencies>
    **<dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>**
    <!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor -->
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>1.5.5.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct -->
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>1.5.5.Final</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>

Trust me you can still use Lombok in your project even if you have excluded it in the Spring Boot Maven plugin configuration. The excludes configuration in the plugin simply tells Maven to exclude the specified artifact from the plugin's classpath. In this case, the lombok artifact is being excluded from the Spring Boot Maven plugin's classpath, but it is not being excluded from your project's classpath.

roudlek
  • 160
  • 9
0

For those, who have the same issue when using mapstruct + lombok
Adding lombok before mapstruct works as Both lombok & Mapstruct are based on annotation-processor, and mapstruct depends on getter & setter generated from lombok to generate the Mapper implementation. On changing the order lombok getter & setter are created first which are then used by mapstruct, so that why changin g order of annotationProcessor works !!

Jay Yadav
  • 236
  • 1
  • 2
  • 10
0

I ran into this as well, and the culprit was the use of

@Accessors(fluent=true)

on my Entity classes. Serves me right for using experimental Lombok features! :)

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
0

I was not sure if this problem still coming for anyone. I have encountered the same problem and the issue is due to invalid mappings. As in your case to map the packaging_id in the map struct interface you should mention like

@Mapping(source = "packaging.field_name" target = "drinkPackaging")

here packaging is the field name of Packaging entity and field_name is whatever the field you want to assign which is declared in your packaging entity. The naming conventions should match exactly means the field name in your entity should be the same as in your mapper interface source attribute.

Dreana
  • 573
  • 4
  • 16
ravi552
  • 1
  • 2
-2

Steps:

  1. Generate Getters / Setters for Drink and DrinkDTO classes(maybe without Lombok).

  2. Build project with Gradle Task: Build

  3. Start project!

Artur Vartanyan
  • 589
  • 3
  • 10
  • 35