Spring Boot: Mapper not being initialized

Viewed 1041

I'm building a Java app with Spring boot, and everything was good until I tried to implement Mappers...

Apparently the mappers are not being initialized. I'm trying to implement this for the first time so I'm not sure if I'm missing something or where the issue is.

This is the issue I get when trying mvn spring-boot:run

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

The ProductMapper class is:

@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());
    }
}

So as far as I read in the documentation, I'm using the @Mapper annotation and defining a default method.

But when I tried to use that in a service class like the following:

@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());
    }

}

Also in my pom.xml I have the following plugins:

<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>

Could you please point me where the issue is?

0 Answers
Related