将 mapstruct 与服务一起使用

using mapstruct with service

我有以下映射器:

@Mapper(componentModel = "spring",
    nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface StudentMapper {

    StudentMapper MAPPER = Mappers.getMapper( StudentMapper.class );

    @Mapping(target = "id", ignore = true)
    Student map(Map<String, Object> map, @MappingTarget Student student);

}

这是我使用 MAPPER 的 StudentService:

import static com.test.StudentMapper.MAPPER;

public class StudentService {
    public void update(final @Valid Map<String, Object> map, Student student) { 
        Student result = MAPPER.map(map, student);
    }
}

当我 运行 我的测试时:

我得到 java.lang.NullPointerException: Cannot invoke map(Object) because "this.qualifier" is null

当我在我的 StudentService class 中使用映射器时,如何正确地注入 Qualifier?

因为您正在使用

componentModel = "spring"

我建议您通过依赖项注入像普通依赖项一样简单地注入映射器。

@Service
@RequiredArgsConstructor
public class StudentService {

    private final StudentMapper studentMapper;

    public void update(final @Valid Map<String, Object> map, Student student) { 
        Student result = MAPPER.updateStudentFromMap(map, student);
    }
}

我不确定您提供的代码是否是使用 mapstruct 的可行方法。

public interface StudentMapper {

StudentMapper MAPPER = Mappers.getMapper( StudentMapper.class );