【问题标题】:How Inyect dependency by interface mapper接口映射器如何依赖 Inyect
【发布时间】: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,我不知道自服务以来如何调用

谢谢!

【问题讨论】:

    标签: spring mapstruct mapper


    【解决方案1】:

    看了很多问题后,我解决了这个表单的错误

    -首先,在pom.xml中添加插件

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source> <!-- depending on your project -->
                    <target>1.8</target> <!-- depending on your project -->
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                        <!-- other annotation processors -->
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
    

    第二次我添加了龙目岛

     <annotationProcessorPaths>
                            <path>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                                <version>1.18.20</version>
                            </path>
                            <path>
                                <groupId>org.mapstruct</groupId>
                                <artifactId>mapstruct-processor</artifactId>
                                <version>1.4.1.Final</version>
                            </path>
    
    

    最后,我添加了自动连线

    @Service
    public class FollowService implements IFollowService{
    
        @Autowired
        IFollowRepository iFollowRepository;
    
        @Autowired
        IUserMapper iUserMapper;
    
    

    【讨论】:

      【解决方案2】:

      iUserMapperFollowService 中的原因是null 是因为您没有注入映射器。

      您需要在您的服务中添加@Autowired

      例如

      @Service
      public class FollowService implements IFollowService{
      
          @Autowired
          IFollowRepository iFollowRepository;
      
          @Autowired
          private IUserMapper iUserMapper;
      
          @Override
          public UserDTOCount countFollowers(int userId) throws UserIdNotFoundException, UserNotFollowException {
              return iUserMapper.toUserCount(iFollowRepository.getUserById(userId));
          }
      
      }
      

      小评论:我的一个小题外话。我建议不要在接口前面加上I。 IDE 可以清楚地显示什么是类和什么是接口,并且在树结构中也更容易看到它们,因为并非所有内容都在“I”下

      【讨论】:

      • 我使用@autowired,但显示错误 bean 未定义,如何将 bean 放在映射器上?
      • 如果你看到这意味着你的映射器没有被组件扫描
      猜你喜欢
      • 2013-02-16
      • 2023-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 2012-10-06
      • 2013-08-28
      • 1970-01-01
      相关资源
      最近更新 更多