Kut*_*tti 8 spring mapper mapstruct
我从这个来源下载了应用程序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);
Run Code Online (Sandbox Code Playgroud)
}
@Data
public class CategoryDTO {
private Long id;
private String name;
}
Run Code Online (Sandbox Code Playgroud)
域类:
@Data
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}
Run Code Online (Sandbox Code Playgroud)
服务等级:
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)
和插件:
<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>
Run Code Online (Sandbox Code Playgroud)
描述:
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.
Run Code Online (Sandbox Code Playgroud)
行动:
Consider defining a bean of type 'guru.springfamework.api.v1.mapper.CategoryMapper' in your configuration.
Run Code Online (Sandbox Code Playgroud)
我认为在我的Intellij批注@Mapper中,不要为mapper创建bean。我没有从John GitHub更改代码。任何的想法?我试图将路径生成的源更改为目标,但这无济于事感谢您的帮助。
小智 8
我通过做解决了错误
还要将此添加到您的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>
Run Code Online (Sandbox Code Playgroud)在您的 Mapper 类中,使用@Mapper(componentModel = "spring")
而不只是@Mapper
并且run mvn clean install
,就是这样!你完成了!
另一种方法是在pom.xml的插件中定义编译器参数,如下所示!
<plugin>
<configuration>
<compilerArgs>
<compilerArg>
-Amapstruct.defaultComponentModel=spring
</compilerArg>
</compilerArgs>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
小智 6
我有同样的问题。在 Gradle 中,我通过添加新的依赖项解决了这个问题:
compileOnly('org.mapstruct:mapstruct-processor:1.3.0.Beta2')
Run Code Online (Sandbox Code Playgroud)
所有必要的依赖项应如下所示:
compile('org.mapstruct:mapstruct:1.3.0.Beta2')
compileOnly('org.mapstruct:mapstruct-processor:1.3.0.Beta2')
annotationProcessor('org.mapstruct:mapstruct-processor:1.3.0.Beta2')
Run Code Online (Sandbox Code Playgroud)
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.4.2.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.2.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
添加这两个依赖项解决了我的问题,创建 bean 所需的处理器(https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor),如果构造函数上的红线打扰您(它不会打扰编译器)@Autowired(required = false)
添加这。
问题是,
默认情况下,使用该@Mapper
构造将生成一个没有@Component 注释的类。
您可能没有使用 Maven 来编译您的项目。确保运行 Mavenclean
和install
,或者mapstruct.defaultComponentModel=spring
手动传入编译器参数。
由于您正在使用:
<compilerArgs>
<compilerArg>
-Amapstruct.defaultComponentModel=spring
</compilerArg>
</compilerArgs>
Run Code Online (Sandbox Code Playgroud)
这将告诉MapStruct
生成一个Spring Component
.
如果您检查已编译的目标类,并在反编译器中打开它(如果您的 IDE 支持此功能),则应该有一个CategoryMapperImpl
用 @Component 注释的。
为了将映射器用作 bean 并使用@Autowired
,只需将映射器声明为@Mapper(componentModel = "spring")
. 然后只需将其注入任何您需要的地方。
归档时间: |
|
查看次数: |
8093 次 |
最近记录: |