【问题标题】:MapStruct not injected in Kotlin projectMapStruct 未注入 Kotlin 项目
【发布时间】:2022-10-16 18:27:44
【问题描述】:

我正在尝试使用 Spring-Boot 创建一个具有当前 Kotlin、MapStruct 和 Java 的项目,并遵循一些在线示例,因为我是 MapStruct 的新手,但是我无法将映射器注入到我的服务中。 Idea 和 Gradle(在构建任务测试中)都抱怨没有找到 bean(UnsatisfiedDependencyException)。谷歌搜索没有帮助。我错过了什么?

MWE:

构建.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.7.4"
    id("io.spring.dependency-management") version "1.0.14.RELEASE"
    kotlin("jvm") version "1.7.20"
    kotlin("plugin.spring") version "1.7.20"
    kotlin("plugin.jpa") version "1.7.20"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17

configurations {
    compileOnly {
        extendsFrom(configurations.annotationProcessor.get())
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    implementation("org.mapstruct:mapstruct:1.5.3.Final")
    annotationProcessor("org.mapstruct:mapstruct-processor:1.5.3.Final")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "17"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

演示应用.kt

package com.example.demo

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args)
}

应用程序.kt

package com.example.demo

import org.mapstruct.Mapper
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import org.springframework.stereotype.Service
import javax.persistence.*

@Entity
@Table(name = "items")
class Item(@Id var id: Int = 0)


data class ItemDto(val id: Int)


@Repository
interface ItemRepo : JpaRepository<Item, Int>


@Mapper(componentModel = "spring")
interface ItemMapper {
    fun entToDto(item: Item) : ItemDto
    fun entsToDtos(items: List<Item>) : List<ItemDto>
    fun dtoToEnt(itemDto: ItemDto) : Item
}

@Service
class Srvc(private val itemMapper: ItemMapper, // XXX: no bean found for this one
           private val repo: ItemRepo)
{
    fun items() = itemMapper.entsToDtos(repo.findAll())
}

// controller skipped

【问题讨论】:

    标签: spring spring-boot kotlin mapstruct


    【解决方案1】:

    Kapt 处于维护模式!参见示例:https://github.com/mapstruct/mapstruct-examples/tree/main/mapstruct-kotlin

    plugin {
        // ...
        kotlin("kapt")
        // ...
    }
    
    dependencies {
        // ...
        kapt("org.mapstruct:mapstruct-processor:$version")
        implementation("org.mapstruct:mapstruct:$version")
        // ...
    }
    

    编辑(由 mpts.cz 提供——对于未来的我或任何像我一样对 Mapstruct 和/或 Gradle 不熟悉的人):

    1. 使用插件kotlin("kapt")
    2. 替换annotationProcessor("org.mapstruct:mapstruct-processor:$version")kapt("org.mapstruct:mapstruct-processor:$version")
    3. 将上一行放在implementation("org.mapstruct:mapstruct:$version")之前

      有了这些变化,一切似乎都可以顺利进行。谢谢努米奇!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-03
      • 2021-10-25
      • 2016-12-12
      • 2021-02-22
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多