Mapstruct 在 Spring Boot 中抛出空指针异常。我该如何解决?

问题描述 投票:0回答:1

我在 Spring Boot 的 mapstruct 中获得空指针豁免,因为它无法初始化。

这是下面显示的错误

java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:233)

我该如何解决?

下面是服务类

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserRepository userRepository;

    private final UserMapper userMapper;

    public Page<UserDTO> getAllUsers(Pageable pageable) {
        return userRepository.findAll(pageable).map(
                userMapper::mapUsertoUserDTO  // HERE IS THE ERROR
        );
    }

这是下面显示的映射器

import org.mapstruct.Mapper;

@Mapper
public interface UserMapper {

    User mapUserDTOtoUser(UserDTO userDTO);

    UserDTO mapUsertoUserDTO(User user);
}

这里是pom.xml中的mapstruct

<properties>
        <java.version>17</java.version>
        <org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
        <org.projectlombok.version>1.18.20</org.projectlombok.version>
        <lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
    </properties>

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${org.projectlombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok-mapstruct-binding</artifactId>
                            <version>${lombok-mapstruct-binding.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>

下面是我的服务测试

@测试 public void userDtoById() {

    // given
    User user = new User();
    // setter

    // when
    when(userRepository.findById(1L)).thenReturn(Optional.of(user));

    UserDTO userDto = userService.getUserById(1L);  // Here is the error

    ...

}

java spring-boot mapstruct
1个回答
0
投票

为了使您的 MapStruct 可用于在 spring 上下文中注入,您需要使用

@Mapper(componentModel = "spring")
public interface UserMapper {

此外,您的

UserServiceImpl.class
使用私有最终字段,因此您还需要一个自动装配的重载构造函数才能将值注入您的字段。

@Autowired
public UserServiceImpl(UserMapper userMapper, UserRepository userRepository) {
this.userRepository =userRepository;
this.userMapper=userMapper;
}
© www.soinside.com 2019 - 2024. All rights reserved.