【发布时间】:2018-01-04 22:39:20
【问题描述】:
我正在尝试运行一个使用 Kotlin 编写并使用 Gradle 构建的小型 Spring Boot 应用程序。当我在 IntelliJ 中运行应用程序时,我收到以下消息:
应用程序启动失败
说明:
com.mycompany.app.rest.api.MyApi 中构造函数的参数1 需要一个 'com.mycompany.app.domain.UserRepository' 类型的 bean 找不到。
行动:
考虑定义一个 bean 类型 'com.mycompany.app.domain.UserRepository' 在您的配置中。
我的APP:
MyAPP directory structure and files
我的应用程序.kt
package com.mycompany.app.startbootapp
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.ComponentScan
@SpringBootApplication
@ComponentScan(basePackages = arrayOf("com.mycompany.app"))
class MyApp
fun main(args: Array<String>) {
runApplication<MyApp>(*args)
}
MyApi.kt
package com.mycompany.app.rest.api
import com.mycompany.app.domain.User
import com.mycompany.app.domain.UserRepository
import com.mycompany.app.infra.ParamUtil
import com.mycompany.app.rest.api.inout.DtoUser
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
@RestController
class MyApi(val paramUtil: ParamUtil, val repo: UserRepository) {
@PostMapping("/user")
fun tokenJwtUsuario(@RequestBody dto: DtoUser, request: HttpServletRequest, response: HttpServletResponse):
ResponseEntity<String> {
if (!paramUtil.verificaParam(dto.id) && !paramUtil.verificaParam(dto.name)) {
return ResponseEntity("Invalid parameter provided !", HttpStatus.BAD_REQUEST)
}
var user = User(dto.id, dto.name)
repo.save(user)
return ResponseEntity(HttpStatus.OK)
}
}
用户.kt
package com.mycompany.app.domain
import javax.persistence.Entity
import javax.persistence.Id
@Entity
data class User(
@Id
val userID: String,
val name: String
) { }
UserRepository.kt
package com.mycompany.app.domain
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface UserRepository: JpaRepository<User, String> {
}
build.gradle
buildscript {
ext {
kotlinVersion = '1.2.10'
springBootVersion = '2.0.0.M7'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
}
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven'
group = 'com.mycompany'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
repositories {
mavenCentral()
mavenLocal()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
configurations {
compile.exclude module: "spring-boot-starter-tomcat"
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("com.fasterxml.jackson.module:jackson-module-kotlin")
compile("org.springframework.boot:spring-boot-starter-undertow")
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-hateoas')
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('org.springframework.kafka:spring-kafka')
compile('org.springframework.boot:spring-boot-starter-webflux')
compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:1.2.10")
compile("org.jetbrains.kotlin:kotlin-reflect")
compile("com.h2database:h2")
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('io.projectreactor:reactor-test')
}
我尝试将 MyApp.kt 移动到 com.mycompany.app 中的上述目录,但没有成功。
【问题讨论】:
-
将主类移动到包
com.mycompany.app,它应该开始工作了。 -
正如我在原帖中提到的,我试过这个,但没有成功。
标签: spring-boot kotlin