如何正确使用MapStruct@Mapping和@Mappings



hi我的问题是,当我使用mvn clean package来构建.jar文件时,我得到了一些错误,看起来像这样:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/Cactus/Documents/GitHub/Microservice/b2b/b2b-warehouse/src/main/java/com/bit/microservices/b2b/warehouse/mapper/ICategoryBpsjMapper.java:[27,33] No property named "createdDate" exists in source parameter(s). Did you mean "reasonDeleted"?
[ERROR] /C:/Users/Cactus/Documents/GitHub/Microservice/b2b/b2b-warehouse/src/main/java/com/bit/microservices/b2b/warehouse/mapper/ICategoryBpsjMapper.java:[27,33] No property named "createdDate" exists in source parameter(s). Did you mean "reasonDeleted"?
[INFO] 2 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  16.438 s
[INFO] Finished at: 2020-11-30T10:55:21+07:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project b2b-warehouse: Compilation failure: Compilation failure: 
[ERROR] /C:/Users/Cactus/Documents/GitHub/Microservice/b2b/b2b-warehouse/src/main/java/com/bit/microservices/b2b/warehouse/mapper/ICategoryBpsjMapper.java:[27,33] No property named "createdDate" exists in source parameter(s). Did you mean "reasonDeleted"?
[ERROR] /C:/Users/Cactus/Documents/GitHub/Microservice/b2b/b2b-warehouse/src/main/java/com/bit/microservices/b2b/warehouse/mapper/ICategoryBpsjMapper.java:[27,33] No property named "createdDate" exists in source parameter(s). Did you mean "reasonDeleted"?
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

但当我通过右键点击Boot Dashboard运行我的项目并启动我的项目时,它会正常运行。我的api运行良好。我试了一下。

第一次尝试我的代码如下所示,我有一个映射器文件如下所示:

@Mapper(componentModel = "spring")
public interface ICategoryBpsjMapper {
ICategoryBpsjMapper INSTANCE = Mappers.getMapper(ICategoryBpsjMapper.class);
@Mappings({ 
@Mapping(target = "createdDate", dateFormat = "dd-MM-yyyy HH:mm:ss"),
@Mapping(target = "modifiedDate",dateFormat = "dd-MM-yyyy HH:mm:ss")})
CategoryBpsjResponseDto entityToDto(CategoryBPSJ entity);
CategoryBPSJ DTOEntity(CategoryBPSJDto dto);

CategoryBPSJ updateEntityFromDto(CategoryBPSJDto dto, @MappingTarget CategoryBPSJ entity);

List<CategoryBpsjResponseDto> entityToDto(List<CategoryBPSJ> entityList);

List<CategoryBPSJ> DTOEnity(List<CategoryBPSJDto> dtoList);
}

这是我的实体:

@Data
@Entity
@Audited
@EntityListeners(AuditingEntityListener.class)
@EqualsAndHashCode(of = "id")
@ToString(of = {"id"})
@Table(name = "msCategoryBPSJ", uniqueConstraints = {
@UniqueConstraint(name = "uk_categoryBPSJName", columnNames = { "categoryBPSJName" })})
public class CategoryBPSJ extends AuditField implements Serializable {
private static final long serialVersionUID = 8062436315663828938L;
@Schema(description = "Id merupakan primary key dan harus disertakan ketika edit, tipe datanya Long", example = "0", required = true)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Schema(description = "kode kategori pengiriman", example = "BPSJCTH1", required = true)
@NotBlank(message = "Tidak Boleh Kosong")
private String categoryBPSCode;
@Schema(description = "nama kategori pengiriman", example = "Toko/Gudang Tujuan Tutup", required = true)
@NotBlank(message = "Tidak Boleh Kosong")
private String categoryBPSJName;

}

这是我的扩展AuditField类:

@MappedSuperclass
@Data
@Audited
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = { "createdDate", "modifiedDate", "isDeleted" }, allowGetters = true, allowSetters = false)
public class AuditField implements Serializable {
private static final long serialVersionUID = 6025082509703710749L;
@Schema(description = "Status di hapus", example = "false", required = false)
public Boolean isDeleted = false;
@Schema(description = "Alasan di hapus", example = "apa pun alasannya", required = false)
public String reasonDeleted = "";
@CreatedDate
private Date createdDate;
@LastModifiedDate
private Date modifiedDate;
@CreatedBy
private String createdBy;
@LastModifiedBy
private String modifiedBy;
}

这是我的DTO AuditFieldDto类:

@Data
public class CategoryBpsjResponseDto extends AuditFieldDto implements Serializable {
private static final long serialVersionUID = -422185482845439241L;
@Schema(description = "Id merupakan primary key dan harus disertakan ketika edit, tipe datanya Long", example = "0", required = true)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
@Schema(description = "kode kategori pengiriman", example = "BPSJCTH1", required = true)
@NotBlank(message = "Tidak Boleh Kosong")
public String categoryBPSCode;
@Schema(description = "nama kategori pengiriman", example = "Toko/Gudang Tujuan Tutup", required = true)
@NotBlank(message = "Tidak Boleh Kosong")
public String categoryBPSJName;
}

这是我的AuditFieldDto类:

@Data
public class AuditFieldDto implements Serializable {
private static final long serialVersionUID = 3758121148184815303L;
@Schema(description = "Status di hapus", example = "false", required = false)
public Boolean isDeleted = false;
@Schema(description = "Alasan di hapus", example = "apa pun alasannya", required = false)
public String reasonDeleted = "";
private String createdDate;

private String modifiedDate;

private String createdBy;

private String modifiedBy;
}

正如你所看到的,AuditField和AuditFieldD有相同的字段,但当我运行mvn clean package时,它会给我带来编译错误,就像我提到的问题的顶部一样。在错误消息中,mapstruct似乎找不到正确的字段。然后我尝试删除@Mappings和@Mapping注释行,构建成功。看起来很可疑。。因为当我使用Boot Dashboard运行时,这些注释运行得很好。

这是我的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bit.microservices</groupId>
<artifactId>b2b</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>b2b-warehouse</artifactId>
<groupId>com.bit.microservices.b2b</groupId>
<name>b2b-warehouse</name>
<description>B2B Warehouse Service</description>
<version>0.0.1-SNAPSHOT</version>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
<lombok.version>1.18.16</lombok.version>
<org.mapstruct.version>1.4.1.Final</org.mapstruct.version>
</properties>
<dependencies>
<dependency>
<groupId>com.bit.microservices</groupId>
<artifactId>model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.bit.b2b</groupId>
<artifactId>security</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-ui -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.4.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.classgraph/classgraph -->
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.8.90</version>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.modelmapper/modelmapper -->
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</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>
<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-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<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>3.8.1</version>
<configuration>
<source>13</source> <!-- depending on your project -->
<target>13</target> <!-- depending on your project -->
<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>
<path>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.3.4.RELEASE</version>
</path>
<!-- other annotation processors -->
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>

如果我想在使用mvn clean package将项目构建到.jar文件时使用@Mappings和@Mapping,那么我在项目中错过了什么?

我在这里找到了罪魁祸首,但我不确定为什么,这是正确的做法吗,因为当我编写这段代码时,它起了作用。好的,让我们开始吧,你可以在我的问题上检查我的mapstruct版本,下面是我如何解决这个问题的:

首先,在我的AuditField和AuditFieldDto中,正如你所看到的,我有2个用";CCD_ 4";数据类型,一开始我把所有字段都写在公共字段中,但后来我的朋友来推荐我把它设为私有字段,但我不小心没有把所有字段设为私有,名为public Boolean isDeleted;public String reasonDeleted;的两个字段是公共字段,这就是为什么错误会通过它们,它们运行良好,错误会转到我的另一行,它是私人写的。

我只是随机悲惨地将他们的数据类型改回了公共字段,正如你所看到的,有4个字段写在了私有字段中,它们是

@CreatedDate
private Date createdDate;
@LastModifiedDate
private Date modifiedDate;
@CreatedBy
private String createdBy;
@LastModifiedBy
private String modifiedBy;

所以我在我的AuditFieldDto和AuditField类中将所有这些都更改为public,我的Audit Field类的完整代码如下所示:

@MappedSuperclass
@Data
@Audited
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = { "createdDate", "modifiedDate", "isDeleted" }, allowGetters = true, allowSetters = false)
public class AuditField implements Serializable {
private static final long serialVersionUID = 6025082509703710749L;
@Schema(description = "Status di hapus", example = "false", required = false)
public Boolean isDeleted;
@Schema(description = "Alasan di hapus", example = "apa pun alasannya", required = false)
public String reasonDeleted;
@CreatedDate
public Date createdDate;
@LastModifiedDate
public Date modifiedDate;
@CreatedBy
public String createdBy;
@LastModifiedBy
public String modifiedBy;
}

它也适用于我的AuditFieldDto类,它看起来像这样:

@Data
public class AuditFieldDto implements Serializable {
private static final long serialVersionUID = 3758121148184815303L;
@Schema(description = "Status di hapus", example = "false", required = false)
public Boolean isDeleted = false;
@Schema(description = "Alasan di hapus", example = "apa pun alasannya", required = false)
public String reasonDeleted = "";
public String createdDate;

public String modifiedDate;

public String createdBy;

public String modifiedBy;
}

当我运行mvn clean package时,构建是成功的!我运行了.jar文件,它起作用了!我想为什么???我因此滞留了好几天。然后我尝试查看生成的文件,您可以在位于target/generated-sources/annotations的项目中找到生成的文件并细化mapper类文件。这是我的,我只举一个生成代码的例子,因为另一个似乎没有任何问题。

以下是GENERATED FILE内部的内容(我们不应该编辑这个文件,只是为了查看(:

@Component
public class ICategoryBpsjMapperImpl implements ICategoryBpsjMapper {
@Override
public CategoryBpsjResponseDto entityToDto(CategoryBPSJ entity) {
if ( entity == null ) {
return null;
}
CategoryBpsjResponseDto categoryBpsjResponseDto = new CategoryBpsjResponseDto();
if ( entity.createdDate != null ) {
categoryBpsjResponseDto.createdDate = new SimpleDateFormat( "dd-MM-yyyy HH:mm:ss" ).format( entity.createdDate );
}
if ( entity.modifiedDate != null ) {
categoryBpsjResponseDto.modifiedDate = new SimpleDateFormat( "dd-MM-yyyy HH:mm:ss" ).format( entity.modifiedDate );
}
categoryBpsjResponseDto.isDeleted = entity.isDeleted;
categoryBpsjResponseDto.reasonDeleted = entity.reasonDeleted;
categoryBpsjResponseDto.createdBy = entity.createdBy;
categoryBpsjResponseDto.modifiedBy = entity.modifiedBy;
return categoryBpsjResponseDto;
}
}

你可以看到他们在没有get和set的情况下访问属性(我指的是getter-setter(,我认为这是导致我错误的罪魁祸首,因为当我将字段标记为private时,如果代码是这样写的,他们将无法访问。现在这是我解决问题的方法。

如果有人有更好的答案,我会把他们的答案作为可接受的答案,如果我的代码不正确或不合适,等待它。

我认为您缺少lombok映射结构绑定。

最新更新