spring 源参数中不存在名为“product.name“的属性,您的意思是“null”吗?-使用lombok和mapstruct时出现问题

pdsfdshx  于 2023-04-19  发布在  Spring
关注(0)|答案(1)|浏览(121)

我正在使用spring Boot 3和java 17构建一个web API。我正在使用lombok和mapstruct,但我遇到了这个错误:

19:48
C:\Users\xxxxx\Documents\workspace\eshop\src\main\java\com\eshop\mapper\ProductMapper.java:19:48
java: No property named "product.name" exists in source parameter(s). Did you mean "null"?
C:\Users\xxxxx\Documents\workspace\eshop\src\main\java\com\eshop\mapper\ProductMapper.java:20:55
java: No property named "product.description" exists in source parameter(s). Did you mean "null"?
C:\Users\xxxxx\Documents\workspace\eshop\src\main\java\com\eshop\mapper\ProductMapper.java:21:53
java: No property named "product.id" exists in source parameter(s). Did you mean "null"?

这是我的mapper:

package com.eshop.mapper;

import com.eshop.domain.entity.Product;
import com.eshop.domain.entity.ProductVariant;
import com.eshop.dto.request.body.ProductCreationBody;
import com.eshop.dto.request.response.ProductGetDetailsResponse;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.util.List;

@Mapper(componentModel = "spring")
public interface ProductMapper {

    ProductGetDetailsResponse map(Product product);

    @Mappings({
            @Mapping(target = "name", source = "product.name"),
            @Mapping(target = "description", source = "product.description"),
            @Mapping(target = "productId", source = "product.id"),
    })
    ProductGetDetailsResponse map(ProductVariant productVariant);

    Product map(ProductCreationBody productCreationRequestBody);

    List<ProductGetDetailsResponse> map(List<ProductVariant> products);


}

这是我试图绘制的物体

package com.eshop.dto.request.response;

import lombok.*;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class ProductGetDetailsResponse {

    private Long id;

    private Long productId;

    private String name;

    private String description;

    private String imageUrl;

    private String color;

    private String size;

    private String price;

    private String discount;

    private String displayOrder;
}
package com.eshop.domain.entity;

import com.eshop.domain.enums.ProductGender;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.time.LocalDate;

@Entity
@Table(name = "product_variant", uniqueConstraints = {
        @UniqueConstraint(columnNames = {"product_id", "productColor", "productSize"})
})
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ProductVariant implements Serializable {

    private static final Long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Enumerated(EnumType.STRING)
    private ProductGender productGender;

    private String productColor;

    private String productSize;

    private Integer unitsInStock;

    private String imageUrl;

    private Double price;

    private Integer discount;

    private LocalDate discountStartDate;

    private LocalDate discountEndDate;

    private Integer displayOrder;

    @ManyToOne
    @JoinColumn(name = "product_id")
    private Product product;

}
package com.eshop.domain.entity;

import com.eshop.domain.enums.ProductStatus;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

@Table(name = "products", uniqueConstraints = {
        @UniqueConstraint(columnNames = "name")
})
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Product implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(unique = true)
    private String name;

    private String description;

    @Enumerated(EnumType.STRING)
    private ProductStatus status;

    @ManyToOne
    @JoinColumn(name = "store_id")
    private Store store;

    @CreationTimestamp
    @Column(name = "created_at")
    private Date createdAt;

    @UpdateTimestamp
    @Column(name = "updated_at")
    private Date updatedAt;

    @OneToMany(mappedBy = "product", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<ProductVariant> productVariants;

    @ManyToOne
    @JoinColumn(name = "subCategory_id")
    private SubCategory subCategory;
}

这是我的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.eshop</groupId>
    <artifactId>eshop</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>eshop</name>
    <description>eshop</description>
    <properties>
        <java.version>17</java.version>
        <version.lombok>1.18.26</version.lombok>
        <version.mapstruct>1.3.1.Final</version.mapstruct>
        <version.mapstruct-lombok>0.2.0</version.mapstruct-lombok>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </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-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>6.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>8.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-envers</artifactId>
            <version>6.1.7.Final</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${version.lombok}</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${version.mapstruct}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-mapstruct-binding</artifactId>
            <version>${version.mapstruct-lombok}</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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${version.lombok}</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${version.mapstruct}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok-mapstruct-binding</artifactId>
                            <version>${version.mapstruct-lombok}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我试图以各种可能的方式将依赖项和插件放入pom.xml中(lombok优先,mapstruct优先,lombok-mapstruct-processor),我试图降级到旧版本的lombok和mapstruct,但它不起作用。
我有错误,然后突然它的工作,然后我添加一个字段到我的Dto和它再次崩溃,但我不能找出是什么使它以前的工作。
当我运行这个应用程序的时候,问题就出现了。清洁安装工作正常,我从lombok和mapstruct中生成了正确的类,所以我真的不明白这里发生了什么。

omqzjyyz

omqzjyyz1#

这是我的Pom.xml
它在以下方面运作良好:
JAVA:17
LOMBOK:1.18.24
Map:1.5.3.最终版

<?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.paulmarcelinbejan</groupId>
        <artifactId>training</artifactId>
        <version>1.0.0</version>
    </parent>

    <artifactId>stack-overflow</artifactId>
    <version>1.0.0</version>

    <properties>
        <org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                    <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>
                        <dependency>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok-mapstruct-binding</artifactId>
                            <version>0.2.0</version>
                        </dependency>
                        <!-- other annotation processors -->
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

相关问题