mapstruct不使用lombok创建构造函数

goqiplq2  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(200)

在我的项目中,我使用lombok和mapstruct。
这是我的实体。
钱包

package com.coin.cointracker.entity;

import lombok.*;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Entity
@Getter
@Setter
@NoArgsConstructor
@RequiredArgsConstructor
public class Wallet {
    //Library
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NonNull
    private Long id;

    @NonNull
    private double InitialHolding;

    @NonNull
    private String currency;

    @OneToMany(mappedBy = "wallet", cascade = CascadeType.REMOVE)
    private Set<Transaction> transactions = new HashSet<>();
}

这是dto,walletresponse

import lombok.*;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class WalletResponse {

    private Long id;

    private double InitialHolding;

    private String currency;
}

钱包Map器

import com.coin.cointracker.dto.response.WalletResponse;
import com.coin.cointracker.entity.Wallet;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.Optional;

@Mapper(componentModel = "spring")
public interface WalletMapper {
    WalletMapper map = Mappers.getMapper(WalletMapper.class);

    //TODO: Is it problem to have Optional as type for parameter?
    WalletResponse walletToWalletResponse(Optional<Wallet> wallet);

    //Wallet walletRequestToWallet(WalletRequest walletRequest);
}

最后是我的pom.xml:

<properties>
        <java.version>1.8</java.version>
        <org.mapstruct.version>1.4.1.Final</org.mapstruct.version>
        <org.projectlombok.version>1.18.16</org.projectlombok.version>
        <lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
</properties>
...
...
<dependencies>
    <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
    </dependency>
    <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
    </dependency>
    <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-mapstruct-binding</artifactId>
            <version>${lombok-mapstruct-binding.version}</version>
    </dependency>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>1.4.1.Final</version>
    </dependency>
</dependencies>

...
...
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <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>1.3.0.Beta2</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.18.4</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

存储库和服务运行良好。存储库成功地从数据库中获取数据,并且服务成功地使用数据库中的数据调用Map器。但mapper无法将walletMap到walletresponse。其结果如下:

{
"id":null,
"InitialHolding":0.00,
"currency":null
}

我哪里出错了?
编辑:这里是生成的walletmapperimpl

package com.coin.cointracker.mapper;

import com.coin.cointracker.dto.response.WalletResponse;
import com.coin.cointracker.entity.Wallet;
import java.util.Optional;
import javax.annotation.Generated;
import org.springframework.stereotype.Component;

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2021-01-25T19:41:00+0300",
    comments = "version: 1.4.1.Final, compiler: javac, environment: Java 1.8.0_261 (Oracle Corporation)"
)
@Component
public class WalletMapperImpl implements WalletMapper {

    @Override
    public WalletResponse walletToWalletResponse(Optional<Wallet> wallet) {
        if ( wallet == null ) {
            return null;
        }

        WalletResponse walletResponse = new WalletResponse();

        return walletResponse;
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题