首页 > 解决方案 > 使用kotlin在SpringBoot中映射结构接口错误

问题描述

我将 java spring boot 代码转换为 kotlin,现在我在 map struct 接口和 bean config 类中有错误

这是我的映射器界面

@Component
@Mapper
interface PersonMapper {

    fun toPerson(personDTO: PersonDTO?): Person?
    fun toPersonDTO(person: Person?): PersonDTO?
    fun toPersons(personDTOS: List<PersonDTO?>?): List<Person?>?
    fun toPersonDTOs(personList: List<Person?>?): List<PersonDTO?>?

    companion object {
        @JvmStatic
        val INSTANCE: PersonMapper = Mappers.getMapper(PersonMapper::class.java)
    }
}

和我的 Bean 配置

@Configuration
class BeanConfig {

    @Bean
    @Primary
    fun personMapper(): PersonMapper {
        return PersonMapper.INSTANCE
    }

}

和我的错误

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personController' defined in file [D:\Zaban\Server\target\classes\ir\ktcoders\zaban\person\PersonController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personService' defined in file [D:\Zaban\Server\target\classes\ir\ktcoders\zaban\person\PersonService.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personMapper' defined in class path resource [ir/ktcoders/zaban/config/BeanConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [ir.ktcoders.zaban.person.PersonMapper]: Factory method 'personMapper' threw exception; nested exception is java.lang.ExceptionInInitializerError

标签: spring-bootkotlindependency-injectionmapstruct

解决方案


首先,您应该@Component从界面中删除 ,因为它没有带来任何价值(但它也不是错误的罪魁祸首)。

很难说更多,但ExceptionInInitializerError看起来Mappers课堂上有问题,所以请提供它的代码。也许它还没有初始化并且找不到这个映射器?


推荐阅读