【发布时间】:2023-04-04 06:51:01
【问题描述】:
我尝试在 java Spring 中使用 mapstruct 将对象 DTO 映射到正常对象
我尝试从服务中调用接口映射器,但我有 NullPointerException,似乎接口没有注入我使用自动装配的服务中,我退出了这个
服务
@Service
public class FollowService implements IFollowService{
@Autowired
IFollowRepository iFollowRepository;
private IUserMapper iUserMapper;
@Override
public UserDTOCount countFollowers(int userId) throws UserIdNotFoundException, UserNotFollowException {
return iUserMapper.toUserCount(iFollowRepository.getUserById(userId));
}
映射器
@Mapper(componentModel = "spring")
public interface IUserMapper {
@Mappings({
@Mapping(source = "id" , target = "id"),
@Mapping(source = "name", target = "name"),
@Mapping(source = "followers", target = "followers", qualifiedByName = "followers")
})
UserDTOCount toUserCount(User user);
错误
processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at com.reto1.demo.Service.FollowService.countFollowers(FollowService.java:54) ~[classes/:na]
我尝试调试,发现 iUserMapper 为 Null,我不知道自服务以来如何调用
谢谢!
【问题讨论】: