MapStruct需要Impl类 [英] MapStruct requires Impl class

查看:966
本文介绍了MapStruct需要Impl类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一节课:

映射器

public interface DeviceTokensMapper {

    DeviceTokensMapper INSTANCE = Mappers.getMapper(DeviceTokensMapper.class);

    @Mappings({
            @Mapping(source = "tokenName", target = "tokenName"),
            @Mapping(source = "userOsType", target = "osType"),
    })

    DeviceTokensDTO toDeviceTokensDTO(DeviceTokens deviceTokens);
}

实体:

@Entity
public class DeviceTokens {

    @Id
    @Column(name="token_name", nullable = false)
    private String tokenName;

    @Column(name = "os", nullable = false)
    @Enumerated
    private UserOSType userOsType;

    public DeviceTokens() {}

    public DeviceTokens(String tokenName, UserOSType userOSType) {
        this.tokenName = tokenName;
        this.userOsType = userOSType;
    }

    public String getTokenName() {
        return tokenName;
    }

    public void setTokenName(String tokenName) {
        this.tokenName = tokenName;
    }

    public UserOSType getUserOsType() {
        return userOsType;
    }

    public void setUserOsType(UserOSType userOsType) {
        this.userOsType = userOsType;
    }
}

DTO:

public class DeviceTokensDTO {

    private String tokenName;

    private UserOSType osType;

    public DeviceTokensDTO() {}

    public DeviceTokensDTO(String tokenName, UserOSType osType) {
        this.tokenName = tokenName;
        this.osType = osType;
    }

    public UserOSType getOsType() {
        return osType;
    }

    public void setOsType(UserOSType osType) {
        this.osType = osType;
    }

    public String getTokenName() {
        return tokenName;
    }

    public void setTokenName(String tokenName) {
        this.tokenName = tokenName;
    }
}

我使用此映射的 spring service 类中的方法:

And method from spring service class where I use this mapping:

@Transactional
public DeviceTokensDTO storeToken(String tokenId, UserOSType userOsType) {
    DeviceTokens deviceTokens = new DeviceTokens(tokenId, userOsType);
    DeviceTokens newDevice = deviceTokensRepository.save(deviceTokens);

    return DeviceTokensMapper.INSTANCE.toDeviceTokensDTO(newDevice);
}

当我运行上述方法时,我看到下一个异常:

When I run above method I see next exception:

错误[dispatcherServlet] :? -Servlet的Servlet.service() 路径为[]的上下文中的[dispatcherServlet]引发了异常[Handler 处理失败;嵌套的异常是 java.lang.ExceptionInInitializerError]的根本原因 java.lang.ClassNotFoundException: dto.DeviceTokensMapperImpl

ERROR [dispatcherServlet]:? - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler processing failed; nested exception is java.lang.ExceptionInInitializerError] with root cause java.lang.ClassNotFoundException: dto.DeviceTokensMapperImpl

那么为什么mapper需要实现类?请问有人可以建议吗? 谢谢.

So why mapper require implementation class? Could please someone advise? Thanks.

推荐答案

MapStruct在编译时生成代码,对Mappers.getMapper(DeviceTokensMapper.class);的调用将查找映射器接口的生成实现.由于某些原因,它似乎在您的部署单元(WAR等)中丢失.

MapStruct generates code at compile time, and the call to Mappers.getMapper(DeviceTokensMapper.class); will look for the generated implementation of the mapper interface. For some reason it seems to be missing in your deployment unit (WAR etc.).

顺便说一句.将Spring用作DI容器时,可以使用@Mapper(componentModel="spring"),并且可以通过依赖项注入而不是使用Mappers工厂来获取映射器实例.

Btw. when working with Spring as your DI container, you can use @Mapper(componentModel="spring") and you will be able to obtain mapper instances via dependency injection instead of using the Mappers factory.

这篇关于MapStruct需要Impl类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆