我正在用Spring Boot
和Hibernate
编写一个MVC
REST
应用程序。我决定使用Mapstruct
进行DTO
映射。似乎我按照指南做了一切,但发出了一个错误。问题出在哪里,我不明白。论坛和谷歌上的信息很少。
附言。起初,我认为问题出在
Lombok
上,所以我删除了Lombok
并手动分配了getters/setters
。然后问题没有解决。我在drine
类和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;
}
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 +
'}';
}
@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);
}
该错误来自这样一个事实,即您试图从列表<>
对象映射属性,但这些对象不存在。只要Mapstruct知道如何映射列表中的元素,Mapstruct就足够聪明,可以在列表之间生成映射器。
因此,您不需要在列表到列表的映射上指定@mapping
注释。Mapstruct将自动使用DrinkToDrinkDto
映射方法。
@Mapping(...)
DrinkDTO drinkToDrinkDTO(Drink drink);
List<DrinkDTO> drinksToDrinksDTO(List<Drink> drinks);
我有点陷入了错误之中 邮递班 Comment.class PostDto。班 评论Dto.class 评论Mapper.class 我试图解决它从过去的6小时,但仍然不明白为什么它显示我下面的错误 任何有用的建议都是解决这个问题的希望。 第二,当需要所有的getter和setter时,只有
我收到Mapstruct异常,其中源和目标中已经存在字段: 来源: 目的地: 例外情况: Pom还有:
是否可以生成自定义的“存在检查”方法名称,作为属性本身的方法而不是拥有对象? 我知道我可以使用hasProperty()方法来检查值的存在...https://map struct . org/documentation/stable/reference/html/# source-presence-check 但是对于Optional或JsonNullable(来自OpenApi nonulla
我们希望能够将未映射的SourcePolicy设置为ReportingPolicy. ERROR,以便默认情况下丢失的源会大声失败。但是,有时源对象模型包含与目标无关的内容。因此,我们希望能够执行以下操作: 可以对目标执行类似操作。如果我尝试上述操作,我会得到错误,因为映射中需要目标。
试图配置两个内存 以及服务业。亚马尔: 但是对于服务中的这四个参数,: 服务“App\Services\Emailer”依赖于不存在的参数“swiftmailer.mailer.spooler”。。。 为什么这个配置不起作用?
如果(tagrecord.gettagid()==null&&tagrecord.gettaglabel()==null),我实际上希望生成的方法返回一个null标记对象。有没有可能,我该如何实现这一点?