开发环境准备:
IDE:spring tools suite 4
Kotlin插件:
-
在sts中打开搜索界面:
2.搜索kotlin,然后点击install:
3.等待安装完成。并且重启sts
开始spring boot项目创建
-
点击file->new->Spring starter project。设置项目名字ID等信息,点击Next
-
这里先就选择web。后续再慢慢加入其他jar包。点击Finish。等待项目安装好
-
项目建号完成。可能会出现以下错误。点击maven->update一下就好
4.最终完成。得到项目。感觉和java项目一样 ,只是文件的后缀改成了.kt
开始hello world
1.新建一个kotlin文件.标准的springMVC注解。因为这里没有使用页面,所以使用@RestController注解直接返回值
2.启动项目。访问本机地址,http://localhost:8080
3。启动的时候可能会报错.
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: @Configuration class 'ShisyatKotlinApplication' may not be final. Remove the final modifier to continue.
Offending resource: edu.shisyat.ShisyatKotlinApplication
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:72) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClass.validate(ConfigurationClass.java:217) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.validate(ConfigurationClassParser.java:208) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:316) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at edu.shisyat.ShisyatKotlinApplicationKt.main(ShisyatKotlinApplication.kt:13) [classes/:na]
出现上面问题只需要在启动类class前面加上open,原因是因为:kotlin中所有类和方法默认都是final的,不能直接继承或重写,而@SpringBootApplication注解中用到@Configuration所以类不可以是final类型,这就导致报错