Spring Boot:Mapper未被初始化

huangapple 未分类评论59阅读模式
标题翻译

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 = &quot;spring&quot;)
public interface ProductMapper {
	
	@Mapping(target = &quot;id&quot;, source = &quot;product.id&quot;)
    @Mapping(target = &quot;name&quot;, source = &quot;product.name&quot;)
    @Mapping(target = &quot;image&quot;, source = &quot;product.image&quot;)
	ProductDTO toProductResponse(Product product);
	
	default List&lt;ProductDTO&gt; toProductDTOList(List&lt;Product&gt; 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&lt;ProductDTO&gt; get(String name, int page, int quantity){
		Page&lt;Product&gt; products = null;
		if(name.length() &gt; 0) {
			Product item = new Product();
			item.setName(&quot;%&quot; + name + &quot;%&quot;);
			
			products = this.productRepository.findAll(Example.of(item), PageRequest.of(page-1, quantity, Sort.by(&quot;name&quot;)));
		} else {
			products = this.productRepository.findAll(PageRequest.of(page-1, quantity, Sort.by(&quot;name&quot;)));
		}

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

}

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

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

Could you please point me where the issue is?

huangapple
  • 本文由 发表于 2020年3月4日 09:44:01
  • 转载请务必保留本文链接:https://java.coder-hub.com/60517961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定