mapstruct doesnt work on my springboot project
added dependencies,path,Mapper annotations (componentModel="spring") in the mapper interface and still get the same error
https://i.stack.imgur.com/CHTPo.png
https://i.stack.imgur.com/nvxo4.png
CodePudding user response:
CategoryMapper is a Entity? if it is kindly add this @NoArgsConstructor becoause its looking for empty constructor
@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
@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
@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")
CodePudding user response:
This is the interface Code @SpicySandwich
`@Mapper(componentModel = "spring") public interface 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);
}`