【问题标题】:Kotlin Spring Boot. Controller class @Validated cause @Autowired of service failsKotlin 弹簧靴。控制器类@Validated 导致@Autowired 的服务失败
【发布时间】:2021-08-07 06:06:50
【问题描述】:

我在HomeController 上使用@Validated,然后用户服务的@Autowired 失败。我得到下面的异常

kotlin.UninitializedPropertyAccessException: lateinit property service has not been initialized
    at com.example.demo.web.HomeController.getService(HomeController.kt:16) ~[main/:na]
    at com.example.demo.web.HomeController.index(HomeController.kt:20) ~[main/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke...
  • Spring Boot 2.4.5
  • 科特林1.5.0
  • 梯度6.8.3-bin

这是我的代码

HoneController.kt

@Validated
@RestController
open class HomeController {

    @Autowired
    lateinit var service: UserSercie

    @RequestMapping("/")
    fun index(@NotNull(message = "name can not be null") name: String): String {
        return "hello " + name + service.getData()
    }
}

UserService.kt

@Service
class UserSercie {

    fun getData(): String {
        return " message from service"
    }
}

build.grade

plugins {
    id 'org.springframework.boot' version '2.4.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.5.0'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

test {
    useJUnitPlatform()
}
compileKotlin {
    kotlinOptions {
        jvmTarget = "11"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "11"
    }
}

【问题讨论】:

    标签: spring-boot validation kotlin service autowired


    【解决方案1】:

    我找到kotlin-spring plugin:https://kotlinlang.org/docs/all-open-plugin.html#spring-support

    我使用插件并删除HomeController 中的open。 然后就可以了。

    buildscript {
    
        repositories {
            mavenCentral()
        }
    
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:$springbootVersion")
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
            classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
        }
    }
    
    subprojects {
    
        apply plugin: 'java'
        apply plugin: 'war'
        apply plugin: 'kotlin'
        apply plugin: "kotlin-spring"
        apply plugin: 'org.springframework.boot'
        apply plugin: 'io.spring.dependency-management'
    
        //other settings
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-10
      • 2019-05-17
      • 2018-02-14
      • 1970-01-01
      • 2021-09-18
      • 2017-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多