gpt4 book ai didi

java - @ map 结构。源参数中不存在名为 "packaging"的属性

转载 作者:行者123 更新时间:2023-12-04 12:34:04 25 4
gpt4 key购买 nike

我正在写一个 MVC REST申请与 Spring BootHibernate .我决定做DTO使用 MAPSTRUCT 映射.好像我按照指南做了所有的事情,但是出现了错误。有什么问题,我无法理解。论坛和谷歌上的信息很少。
附言起初我以为问题出在Lombok ,所以我删除了 Lombok并手动分配 getters / setters .然后问题没有解决。我在 Drink 中都拿走了类和在 DrinkDTO我开了getters / setters .它仍然没有帮助。
喝:

@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;
}
喝DTO:
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 +
'}';
}
客户 Controller :
@GetMapping("/drinks")
List<DrinkDTO> getAllDrinks(){
return DrinkMapper.INSTANCE.drinksToDrinksDTO(customerService.getAllDrinks());
}
构建.gradle
// Mapstruct
implementation 'org.mapstruct:mapstruct:1.3.1.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.3.1.Final'
饮料映射器:
@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);
}
错误:
enter image description here

最佳答案

该错误来自于您尝试从 List<> 映射属性这一事实。对象,但那些不存在。 Mapstruct 足够智能,可以在列表之间生成映射器,前提是它知道如何映射列表内的元素。
所以你不需要指定 @Mapping列表到列表映射上的注释。 Mapstruct 将使用 drinkToDrinkDTO自动映射方法。

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

List<DrinkDTO> drinksToDrinksDTO(List<Drink> drinks);

关于java - @ map 结构。源参数中不存在名为 "packaging"的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63034956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com