gpt4 book ai didi

spring - MapStruct - @Mapper 注释不创建 bean

转载 作者:行者123 更新时间:2023-12-04 11:00:39 30 4
gpt4 key购买 nike

我从这个来源下载了应用程序 https://github.com/springframeworkguru/spring5-mvc-rest/tree/vendor-api
我对 MapStruct 有问题。

@Mapper
public interface CategoryMapper {

CategoryMapper INSTANCE = Mappers.getMapper(CategoryMapper.class);


CategoryDTO categoryToCategoryDTO(Category category);

}
@Data
public class CategoryDTO {
private Long id;
private String name;
}

域类:
@Data
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}

服务等级:
@Service
public class CategoryServiceImpl implements CategoryService {

private final CategoryMapper categoryMapper;
private final CategoryRepository categoryRepository;

public CategoryServiceImpl(CategoryMapper categoryMapper, CategoryRepository categoryRepository) {
this.categoryMapper = categoryMapper;
this.categoryRepository = categoryRepository;
}
}

在 pom.xml 依赖项中,我只粘贴了两个::
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<org.mapstruct.version>1.2.0.CR2</org.mapstruct.version>
</properties>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>

和插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<compilerArg>
-Amapstruct.defaultComponentModel=spring
</compilerArg>
</compilerArgs>
</configuration>
</plugin>

描述:
Parameter 0 of constructor in 
guru.springfamework.services.CategoryServiceImpl required a bean of type 'guru.springfamework.api.v1.mapper.CategoryMapper' that could not be found.

行动:
Consider defining a bean of type 'guru.springfamework.api.v1.mapper.CategoryMapper' in your configuration.

我认为在我的 Intellij 注释中,@Mapper 不要为映射器创建一个 bean。
我没有更改 John GitHub 的代码。
任何的想法?
我试图将路径生成的源更改为目标,但这无济于事
感谢帮助。

最佳答案

我通过执行解决了错误

  • mvn 全新安装
  • 也将此添加到您的 pom
       <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    <annotationProcessorPaths>
    <path>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>${lombok.version}</version>
    </path>
    <path>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>${org.mapstruct.version}</version>
    </path>
    </annotationProcessorPaths>
    <compilerArgs>
    <compilerArg>
    -Amapstruct.defaultComponentModel=spring
    </compilerArg>
    </compilerArgs>
    </configuration>
    </plugin>
  • 关于spring - MapStruct - @Mapper 注释不创建 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47109827/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com