【问题标题】:map struct interface error in SpringBoot with kotlin使用 kotlin 在 Spring Boot 中映射结构接口错误
【发布时间】:2021-04-16 16:47:24
【问题描述】:

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

这是我的 Mapper 界面

@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-boot kotlin dependency-injection mapstruct


    【解决方案1】:

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

    很难说更多,但是ExceptionInInitializerError 看起来Mappers 类有问题,所以请提供它的代码。可能还没有初始化,找不到这个映射器?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-26
      • 2022-10-12
      • 2021-10-24
      • 2019-02-03
      • 2018-06-21
      • 2019-08-22
      • 1970-01-01
      相关资源
      最近更新 更多