Spring Boot: Mapper не инициализируется - PullRequest
1 голос
/ 04 марта 2020

Я создаю Java приложение с загрузкой Spring, и все было хорошо, пока я не попытался реализовать Mappers ...

Очевидно, что мапперы не инициализируются. Я пытаюсь реализовать это в первый раз, поэтому я не уверен, что я что-то упустил или где проблема.

Это проблема, которую я получаю при попытке mvn spring-boot:run

Compilation failure: 
[ERROR] /service/impl/ProductServiceImpl.java:[33,23] variable productMapper not initialized in the default constructor

Класс ProductMapper:

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

    @Mapping(target = "id", source = "product.id")
    @Mapping(target = "name", source = "product.name")
    @Mapping(target = "image", source = "product.image")
    ProductDTO toProductResponse(Product product);

    default List<ProductDTO> toProductDTOList(List<Product> productList) {
        if (productList == null || productList.isEmpty()) return emptyList();
        return productList.stream().map(this::toProductResponse).collect(toList());
    }
}

Итак, насколько я читаю в документации, я использую аннотацию @Mapper и определяю метод default.

Но когда я попытался использовать это в классе обслуживания, как показано ниже:

@Service
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
public class ProductServiceImpl implements ProductService {

    ProductMapper productMapper;

@Override
    public List<ProductDTO> get(String name, int page, int quantity){
        Page<Product> products = null;
        if(name.length() > 0) {
            Product item = new Product();
            item.setName("%" + name + "%");

            products = this.productRepository.findAll(Example.of(item), PageRequest.of(page-1, quantity, Sort.by("name")));
        } else {
            products = this.productRepository.findAll(PageRequest.of(page-1, quantity, Sort.by("name")));
        }

        return productMapper.toProductDTOList(products.toList());
    }

}

Также в моем пом. xml У меня есть следующие плагины:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>pre-integration-test</id>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-integration-test</id>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*Int.java</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Может пожалуйста, укажите мне, где проблема?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...