英文:
@MAPSTRUCT. No property named "packaging" exists in source parameter(s)
问题
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 = "drinkCountries", target = "countries")
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:
英文:
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);
}
答案1
得分: 75
对于那些在使用mapstruct
+ lombok
时遇到相同问题的人:
我也曾遇到相同的问题。原因是我也使用了Lombok
插件。
无需删除它,但您必须确保在pom.xml
中的<annotationProcessorPaths>
中,Lombok标签位于Mapstruct标签之前。
示例(pom.xml文件的一部分):
<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> <!-- 重要 - LOMBOK在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>
英文:
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>
答案2
得分: 7
以下是您要翻译的内容:
只是要补充一下 @Jakub Słowikowski 的回答,gradle 依赖项也会发生相同的情况。以下的顺序导致了错误:
dependencies {
...
annotationProcessor("org.mapstruct:mapstruct-processor:${mapstructVersion}")
annotationProcessor("org.projectlombok:lombok:${lombokVersion}")
...
}
因此,我被迫调整顺序:
dependencies {
...
annotationProcessor("org.projectlombok:lombok:${lombokVersion}")
annotationProcessor("org.mapstruct:mapstruct-processor:${mapstructVersion}")
...
}
英文:
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}")
...
}
答案3
得分: 6
错误是因为您尝试映射List<>
对象的属性,但这些属性不存在。Mapstruct足够智能,可以在知道如何映射列表内元素的情况下生成列表之间的映射器。
因此,您不需要在列表到列表的映射上指定@Mapping
注释。Mapstruct将自动使用drinkToDrinkDTO
映射方法。
@Mapping(...)
DrinkDTO drinkToDrinkDTO(Drink drink);
List<DrinkDTO> drinksToDrinksDTO(List<Drink> drinks);
英文:
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);
答案4
得分: 1
在我的情况下,错误不是由注解处理器的声明顺序引起的,而是由于缺少Lombok注解引起的。
我有一个将Customer
映射到CustomerDto
的映射,但Customer
缺少了getter和setter方法。
CustomerDto toCustomerDto(Customer customer);
使用以下代码
@Data
public class CustomerDto {
...
}
以及
public class Customer {
...
}
为Customer
类添加@Data
解决了我的问题。
英文:
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.
答案5
得分: 0
尝试在@Mapper注解中添加一个uses参数,如果您还有PackagingMapper、CoutriesMapper等,可以像这样:
@Mapper(uses = {PackagingMapper.class, CountriesMapper.class, ManufacturerMapper.class})
public interface DrinkMapper{
....
}
英文:
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{
....
}
答案6
得分: 0
只需将Lombok依赖放在您的pom.xml文件中的第一个位置,然后重新加载Maven项目。您不需要添加Mapstruct插件,我只是使用了依赖项。
<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>
信任我,即使您在Spring Boot Maven插件配置中排除了它,您仍然可以在项目中使用Lombok。插件中的排除配置只是告诉Maven从插件的类路径中排除指定的工件。在这种情况下,Lombok工件被排除在Spring Boot Maven插件的类路径之外,但未被排除在您项目的类路径之外。
英文:
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.
答案7
得分: 0
对于那些在使用mapstruct + lombok
时遇到相同问题的人<br/>
在mapstruct
之前添加lombok
可以解决问题</b>
lombok
和Mapstruct
都基于annotation-processor
,而mapstruct
依赖于从lombok生成的getter和setter来生成Mapper实现。改变lombok getter和setter生成的顺序,然后再由mapstruct使用,这就是改变annotationProcessor
顺序的原因!!
英文:
For those, who have the same issue when using mapstruct + lombok
<br/>
<b>Adding lombok before mapstruct works as</b>
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 !!
答案8
得分: 0
我也遇到了这个问题,罪魁祸首是在我的实体类上使用了
@Accessors(fluent=true)
对我来说,使用实验性的Lombok功能是我的错!
英文:
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!
答案9
得分: 0
我不确定这个问题是否仍然会影响任何人。
我遇到了相同的问题,问题是由于无效的映射导致的。
就像在你的情况下,要映射映射结构接口中的packaging_id
,你应该像这样提到:
@Mapping(source = "packaging.field_name", target = "drinkPackaging")
这里的packaging
是Packaging
实体的字段名,而field_name
是你想要分配的字段,它在你的包装实体中声明。
命名约定应该完全匹配,这意味着你的实体中的字段名应该与你的映射接口源属性中的字段名相同。
英文:
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.
答案10
得分: -2
-
生成
Drink
和DrinkDTO
类的Getters / Setters
(可能不使用Lombok
)。 -
使用
Gradle
任务:Build
构建项目。 -
启动项目!
英文:
Steps:
-
Generate
Getters / Setters
forDrink
andDrinkDTO
classes(maybe withoutLombok
). -
Build project with
Gradle
Task:Build
-
Start project!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论