我正在尝试将Spring Boot项目从Kotlin 1.2.71迁移到1.3.0。 当我更新Kotlin版本时,应用程序上下文无法通过以下堆栈跟踪加载:
[...]
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'universityController' method
public final com.vindedu.api.view.university.UniversityApplicationOverview com.vindedu.api.controller.UniversityController.getApplications(org.springframework.security.core.userdetails.UserDetails,boolean)
to {[/uni/applications],methods=[GET]}: There is already 'universityController' bean method
public static com.vindedu.api.view.university.UniversityApplicationOverview com.vindedu.api.controller.UniversityController.getApplications$default(com.vindedu.api.controller.UniversityController,org.springframework.security.core.userdetails.UserDetails,boolean,int,java.lang.Object) mapped.
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:576)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:540)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:264)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:250)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:214)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:184)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:127)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 98 more
我的UniversityController
类中的以下函数映射到/uni/applications/...
:
@ApiOperation("Get applications received from students", tags = ["University API"],
authorizations = [Authorization(value = "basicAuth")])
@RequestMapping(method = [RequestMethod.GET], path = ["/uni/applications"])
fun getApplications(@AuthenticationPrincipal user: UserDetails, @RequestParam("visible") visible: Boolean = true): UniversityApplicationOverview {
return universityMessagingService.getApplications(user, visible)
}
@ApiOperation("Toggle application visibility for university", tags = ["University API"],
authorizations = [Authorization(value = "basicAuth")])
@RequestMapping(method = [RequestMethod.PUT], path = ["/uni/applications/{id}/visible"])
fun toggleApplicationVisibility(@AuthenticationPrincipal user: UserDetails,
@PathVariable id: Long): UniversityApplicationDetails {
return universityMessagingService.toggleApplicationVisibility(user, id)
}
@ApiOperation("Get application received from student", tags = ["University API"],
authorizations = [Authorization(value = "basicAuth")])
@RequestMapping(method = [RequestMethod.GET], path = ["/uni/applications/{id}"])
fun getApplication(@AuthenticationPrincipal user: UserDetails,
@PathVariable id: Long): UniversityApplicationDetails {
return universityMessagingService.getApplication(user, id)
}
@ApiOperation("Update application status", tags = ["University API"],
authorizations = [Authorization(value = "basicAuth")])
@RequestMapping(method = [RequestMethod.PUT], path = ["/uni/applications/{id}/status"])
fun updateApplicationStatus(@AuthenticationPrincipal user: UserDetails,
@PathVariable id: Long,
@Valid @RequestBody updatedStatus: ApplicationStatusUpdate): UniversityApplicationDetails {
return universityMessagingService.updateApplicationStatus(user, id, updatedStatus)
}
当我删除上面列出的映射时,我在运行时看到了另一个控制器的相同堆栈跟踪。
该项目在Kotlin 1.2.71中完美运行。我非常感谢任何建议,可以使其与Kotlin 1.3.0一起使用!我的完整build.gradle
在下面列出。
请注意:
我使用以下JDK编译并运行此项目:
java version "11" 2018-09-25 Java(TM) SE Runtime Environment 18.9
(build 11+28) Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11+28,
mixed mode)
由于项目中的类依赖于Spring Boot 1.x,因此我无法更新Spring Boot。
build.gradle:
buildscript {
ext.kotlin_version = '1.3.0' // Was '1.2.71'
ext.spring_boot_version = '1.5.4.RELEASE'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // Required for Kotlin integration
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
}
}
apply plugin: 'idea'
apply plugin: 'kotlin'
apply plugin: 'kotlin-allopen'
apply plugin: 'kotlin-noarg'
apply plugin: 'org.springframework.boot'
apply plugin: 'application'
allOpen {
annotation("org.springframework.boot.autoconfigure.SpringBootApplication")
annotation("org.springframework.stereotype.Service")
annotation("org.springframework.context.annotation.Configuration")
}
noArg {
annotation("javax.persistence.Entity")
}
jar {
baseName = 'vindedu-api'
version = '0.0.1'
}
repositories {
jcenter()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // Required for Kotlin integration
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" // Required for Kotlin integration
compile 'com.fasterxml.jackson.module:jackson-module-kotlin:2.9.2'
compile 'org.springframework.boot:spring-boot-starter-security'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.springframework.session:spring-session:1.3.1.RELEASE'
compile 'io.springfox:springfox-swagger2:2.7.0'
compile 'io.springfox:springfox-swagger-ui:2.7.0'
compile 'org.postgresql:postgresql:42.1.4'
compile 'org.flywaydb:flyway-core:4.2.0'
// JAXB dependencies don't ship with the JDK anymore
compile 'javax.xml.bind:jaxb-api:2.3.1'
compile 'com.sun.xml.bind:jaxb-impl:2.3.1'
compile 'javax.activation:activation:1.1.1'
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile 'com.h2database:h2:1.4.196'
}
task wrapper(type: Wrapper) {
gradleVersion = '4.10.2'
}
springBoot {
mainClass = 'com.vindedu.api.ApplicationKt'
}
bootRun {
systemProperty("PROP_NAME", "prop-value")
}
test {
maxParallelForks = Runtime.runtime.availableProcessors() / 3
}
答案 0 :(得分:3)
从1.3.0开始,Kotlin编译器不会为默认方法生成桥接标志,并且新的字节码仅在较新的Spring Boot版本中受支持。请升级到Spring Boot2。参考问题:https://youtrack.jetbrains.com/issue/KT-27947