MapStruct 实现在 Spring Boot Web 应用程序中不起作用

Ank*_*kit 7 java entity dto spring-boot mapstruct

我是 Spring Boot 和MapStruct Tool的新手。

早些时候,一个项目(由其他团队使用这些技术编写)没有启动。然后,我在 Mapper Abstract Class 中做了一些更改,但现在 mapper 对象在应用程序启动时变为 null。

映射器抽象类:

@Mapper(componentModel = "spring")
public abstract class UserAndEmployeeMapper {

    public UserAndEmployeeMapper INSTANCE = Mappers.getMapper( UserAndEmployeeMapper.class );

    @Mapping(source = "username", target = "name")
    @Mapping(source = "ssn", target = "ssn", defaultValue = "xxxxxx" )
    @Mapping(target = "salary", constant = "34.67")
    @Mapping(target = "dob", dateFormat = "dd/MM/yyyy", constant = "10/12/2002")
    public abstract Employee mapToEmployee(User user);

    public abstract List<Employee> mapToEmployee(List<User> users);

    @Mapping(source = "name", target = "username")
    public abstract User mapToUser(Employee employee);

    public abstract List<User> mapToUser(List<Employee> employees);

}
Run Code Online (Sandbox Code Playgroud)

LoginServiceImpl 类

@Service("loginService")
public class LoginServiceImpl implements LoginService{

    private static final AtomicLong counter = new AtomicLong();

    @Autowired
    private EmployeeDao employeeDao;

    private UserAndEmployeeMapper userAndEmployeeMapper;
...

}
Run Code Online (Sandbox Code Playgroud)

pom.xml

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.jdk8.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
</build>
Run Code Online (Sandbox Code Playgroud)

在 LoginServiceImpl 添加 @Autowired 后,应用程序未启动并显示以下错误日志

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userAndEmployeeMapper in org.service.impl.LoginServiceImpl required a bean of type 'org.mapper.UserAndEmployeeMapper' that could not be found.


Action:

Consider defining a bean of type 'org.mapper.UserAndEmployeeMapper' in your configuration.
Run Code Online (Sandbox Code Playgroud)

有什么建议 ?

小智 10

在 pom.xml 中尝试一下:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>17</source>
                <target>17</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>1.4.2.Final</version>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>1.18.22</version>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok-mapstruct-binding</artifactId>
                        <version>0.2.0</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

并重新加载 Maven (ALT+F5)


Fil*_*lip 6

首先,public UserAndEmployeeMapper INSTANCE = Mappers.getMapper( UserAndEmployeeMapper.class );应该只与默认组件模型一起使用,否则您可能会面临UserAndEmployeeMapper未正确初始化的风险。

UserAndEmployeeMapperLoginServiceImpl必须被标注@Autowired,否则就不能被Spring注入,这就是为什么它是null

我不知道你的包结构。如果您的 Spring Boot 应用程序类在包中,org那么它将选择UserAndEmployeeMapperImpl. 否则请确保弹簧配置拿起UserAndEmployeeMapperImpl.

如果上面的所有内容都正确设置并且您通过 IDE 启动应用程序,请确保target/generated-sourcesGradle 或 Gradle 的替代方案是您的源代码的一部分并被选中。查看IDE 支持以确保您为 IDE 正确设置了注解处理器发现。例如,IntelliJ 不会使用您当前的设置调用 MapStruct Annotation Processor,它不会annotationProcessorPaths从 maven 编译器中获取。


Ank*_*kit 4

将抽象类作为接口对我有用。

public interface UserAndEmployeeMapper {
Run Code Online (Sandbox Code Playgroud)

  • 对于任何无法找到映射器失败位置的人,设置 _unmappedTargetPolicy_ 也很有用(例如 `@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.WARN)` ) (7认同)