[英]Consider defining a bean of type 'Mapper' in your configuration [Spring-Boot]
mapstruct doesn't work on my Spring Boot project. mapstruct 不适用于我的 Spring 引导项目。
I added dependencies, path, Mapper annotations (componentModel="spring") in the mapper interface and still get the same error我在mapper接口中添加了依赖、路径、Mapper注解(componentModel="spring"),仍然报同样的错误
https://i.stack.imgur.com/CHTPo.png https://i.stack.imgur.com/CHTPo.png
https://i.stack.imgur.com/nvxo4.png https://i.stack.imgur.com/nvxo4.png
CategoryMapper is a Entity? CategoryMapper 是实体吗? if it is kindly add this @NoArgsConstructor becoause its looking for empty constructor
如果它是请添加这个 @NoArgsConstructor 因为它正在寻找空构造函数
@NoArgsConstructor
public class CategoryMapper {
}
if you dont have lombok just add this如果你没有龙目岛只需添加这个
//empty constructor
public CategoryMapper() {
}
If this class is Repository make sure to hava @Mapper如果这个 class 是 Repository 确保有 @Mapper
@Mapper
public class CategoryMapper {
}
Dont forget also do add this to SpringApplication runner class. This class is generated when you first created the project file不要忘记将它添加到 SpringApplication runner class。这个 class 是在您第一次创建项目文件时生成的
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
//For repsitory dont forget this.
//This will be added upper of the class in springapplication class runner
@MappedTypes({YourEntity.class})
@MapperScan("package patch to you repository")
This is the interface Code @SpicySandwich这是接口代码@SpicySandwich
`@Mapper(componentModel = "spring") public interface SubredditMapper { `@Mapper(componentModel = "spring") 公共接口 SubredditMapper {
@Mapping(target = "numberOfPosts", expression = "java(mapPosts(subreddit.getPosts()))")
SubredditDto mapSubredditToDto(Subreddit subreddit);
default Integer mapPosts(List<Post> numberOfPosts) {
return numberOfPosts.size();
}
@InheritInverseConfiguration
@Mapping(target = "posts", ignore = true)
Subreddit mapDtoToSubreddit(SubredditDto subredditDto);
}` }`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.