嗨,我的问题是当我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和AuditFieldDto具有相同的字段,但是当我运行时mvn clean package
,给我COMPILATION ERROR,就像我提到的问题的顶部一样。在错误消息中,似乎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>
如果在将项目构建为.jar文件时要使用@Mappings和@Mapping,我在项目中错过了什么mvn clean package
?
我在这里找到了罪魁祸首,但是我不确定为什么这样做是正确的事情,因为当我执行此代码时,它就起作用了。好的,让我们开始吧,您可以在我的问题中检查我的mapstruct版本,这是我如何解决的问题:
首先,在我的AuditField和AuditFieldDto中,如您所见,我有2个用“ public
”数据类型编写的字段,起初我将所有字段都公开编写,但后来我的朋友来推荐我将其设为私有,但是我无意间没有将所有内容都设为私有,这两个字段分别命名为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类中将它们全部更改为公开,我的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;
@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
映射器类文件并对其进行优化。这是我的,我只举一个生成代码的例子,因为另一个似乎没有任何问题。
这是里面的内容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),我认为这是导致我出错的元凶,因为当我将字段标记为私有时,如果代码是这样写的,他们将无法被访问。现在这就是我解决它的方法。
如果有人有更好的答案,我将把他们的答案作为接受的答案,如果我的代码不正确或不合适,请等待。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句