Spring Boot :无法解析“Product”中的方法“builder”

2nc8po8w  于 2023-10-15  发布在  Spring
关注(0)|答案(2)|浏览(162)

我在Product class中使用了lombok的@builder anotation。但是当我使用Product.builder()时,它不是工作。此外,getter和setter无法解析。我已经尝试降级一些Lombok岛版本,仍然不工作。
这是我在ProductService中的代码:

package dev.beyondnguyen.productservice.service;

import dev.beyondnguyen.productservice.dto.ProductRequest;
import dev.beyondnguyen.productservice.model.Product;
import dev.beyondnguyen.productservice.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductService {

    @Autowired
    private ProductRepository productRepository;
    public void createProduct(ProductRequest productRequest) {
        Product product = Product.builder()
                .name(productRequest.getName())
                .description(productRequest.getDescription())
                .price(productRequest.getPrice())
                .build();

        productRepository.save(product);
        log.info("Product {} is saved", product.getId());
    }

}

Product类中的代码:

package dev.beyondnguyen.productservice.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.math.BigDecimal;

@Document(collection = "products")
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
public class Product {
    @Id
    private String id;
    private String name;
    private String description;
    private BigDecimal price;
}

这是怎么回事?我该如何解决?

yyyllmsg

yyyllmsg1#

这可能是你导入的maven/gradle依赖中的一个问题。请确保项目lombok依赖项导入为
Gradle:

compileOnly group: 'org.projectlombok', name: 'lombok', version: 'yourVersionOfLombok'

annotationProcessor 'org.projectlombok:lombok:yourVersionOfLombok'

梅芬:

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>yourVersionOfLombok</version>
<scope>provided</scope>
<annotationProcessorPaths>
                <path>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                    <version yourVersionOfLombok</version>
                </path>                         
            </annotationProcessorPaths>

就像这样。如果对你有帮助就告诉我。

gblwokeq

gblwokeq2#

如果您使用IntelliJ IDEA,则需要安装Lombok插件,以便它能够理解项目的Lombok库(Gradle/Maven)的注解。

相关问题