标题翻译
Spring Boot: Mapper not being initialized
问题
我正在使用Spring Boot构建一个Java应用程序,一切都很顺利,直到我尝试实现Mappers...
显然,这些映射器没有被初始化。这是我第一次尝试实现,所以我不确定是否漏掉了什么地方或者问题出在哪里。
这是我在尝试运行mvn spring-boot:run
时遇到的问题:
编译失败:
[ERROR] /service/impl/ProductServiceImpl.java:[33,23] 变量productMapper在默认构造函数中未初始化
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());
}
}
此外,在我的pom.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>
请问您能指出问题出在哪里吗?
英文翻译
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?
专注分享java语言的经验与见解,让所有开发者获益!
评论