使用 Kotlin
结合 SpringBoot
开发一个 RESTFul
版本的 HelloWorld
。
如果单单Kotlin 我是不相信在短时间内有产生多大的风波,但是如果Kotlin 能和 Java 开发神器 SpringBoot 结合起来,我感觉能飞。。。
1.首先新建 Gradle
的 Kotlin
工程
打开IDEA ,File->New->Project
输入相关信息之后下一步:
一般 GroupId
为公司域名反着写,加上项目名字
建议选中 Useauto-import
,自动导包 ,使用本地 Gradle
,但是需要先安装Gradle(https://gradle.org/install/)
如果没有选择 使用本地 Gradle 项目创建完成之后Gradle的包需要下载,这个时间有点长,以前做Android就深受其苦,所有要做好心理准备,除非你有一个好用的FQ工具。
项目创建完成之后会生成一个 Gradle
文件 build.gradle
和 settings.gradle
, settings.gradle
文件我们暂且不管,先看看 build.gradle
文件:
-
group
'name.quanke.kotlin.restful'
-
-
version
'1.0-SNAPSHOT'
-
-
buildscript
{
-
-
ext
.kotlin_version =
'1.2.10'
-
-
repositories
{
-
-
mavenCentral
()
-
-
}
-
-
dependencies
{
-
-
classpath
"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
-
-
}
-
-
}
-
-
apply plugin
: 'kotlin'
-
-
repositories
{
-
-
mavenCentral
()
-
-
}
-
-
dependencies
{
-
-
compile
"org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
-
-
}
-
-
compileKotlin
{
-
-
kotlinOptions
.jvmTarget =
"1.8"
-
-
}
-
-
compileTestKotlin
{
-
-
kotlinOptions
.jvmTarget =
"1.8"
-
-
}
下载依赖和插件也是一个漫长痛苦的过程。各位不要心急,慢慢来,一次不行多试几次。
从生成的配置文件看, IDEA
已经自动把 Gradle
构建 Kotlin
工程插件 kotlin-gradle-plugin
,以及 Kotlin
标准库 kotlin-stdlib
添加到 build.gradle
文件中了。
2.配置 build.gradle
文件
-
group
'name.quanke.kotlin.rest'
-
-
version
'1.0-SNAPSHOT'
-
-
buildscript
{
-
-
ext
.kotlin_version
=
'1.2.10'
-
-
ext
.spring_boot_version
=
'1.5.4.RELEASE'
-
-
repositories
{
-
-
mavenCentral
()
-
-
}
-
-
dependencies
{
-
-
classpath
"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
-
-
classpath
"org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
// See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin
-
-
// org.jetbrains.kotlin:kotlin-allopen 是全开放编译器插件。我们使用Kotlin 调用Java的Spring AOP框架和库,需要类为 open(可被继承实现),而Kotlin 类和函数都是默认 final 的,这样我们需要为每个类和函数前面加上open修饰符。
-
-
// 这样的代码写起来,可费事了。还好,我们有all-open 编译器插件。它会适配 Kotlin 以满足这些框架的需求,并使用指定的注解标注类而其成员无需显式使用 open 关键字打开。 例如,当我们使用 Spring 时,就不需要打开所有的类,跟我们在Java中写代码一样,只需要用相应的注解标注即可。
-
-
classpath
"org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
-
-
}
-
-
}
-
-
apply plugin
: 'kotlin'
-
-
apply plugin
: "kotlin-spring" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin
-
-
apply plugin
: 'org.springframework.boot'
-
-
jar
{
-
-
baseName
= '0_2RestfulApi-service'
-
-
version
= '0.1.0'
-
-
}
-
-
repositories
{
-
-
mavenCentral
()
-
-
}
-
-
dependencies
{
-
-
compile
"org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
-
-
compile
'org.springframework.boot:spring-boot-starter-web'
-
-
testCompile
('org.springframework.boot:spring-boot-starter-test'
)
-
-
}
-
-
compileKotlin
{
-
-
kotlinOptions
.jvmTarget
=
"1.8"
-
-
}
-
-
compileTestKotlin
{
-
-
kotlinOptions
.jvmTarget
=
"1.8"
-
-
}
3.创建包
在kotlin目录下面创建:
name.quanke.kotlin.rest
注意:不能直接在kotlin目录下面写Application 类,不然会报错
Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package
3.创建数据模型Greeting类:
package name.quanke.kotlin.rest
/**
* Created by http://quanke.name on 2018/1/9.
*/
data class Greeting(val id: Long, val content: String)
如果是Java写的话,想象一下这需要多少行代码,看看kotlin,不明则厉。。。
5.创建GreetingController
package name.quanke.kotlin.rest
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.util.concurrent.atomic.AtomicLong
/**
* Created by http://quanke.name on 2018/1/9.
*/
@RestController
class GreetingController {
val counter = AtomicLong()
@GetMapping("/greeting")
fun greeting(@RequestParam(value = "name", defaultValue = "World") name: String) =
Greeting(counter.incrementAndGet(), "Hello, $name")
}
6.创建 Application
-
package
name.
quanke.
kotlin.
rest
-
-
import
org.
springframework.
boot.
SpringApplication
-
-
import
org.
springframework.
boot.
autoconfigure.
SpringBootApplication
-
-
/**
-
-
* Created by http://quanke.name on 2018/1/9.
-
-
*/
-
-
@SpringBootApplication
-
-
class
Application
-
-
fun main
(args
:
Array<
String>)
{
-
-
SpringApplication
.run
(Application
::class
.java
,
*args
)
-
-
}
点击Gradle的bootRun:
或者点击
如果没有毛病,访问
http://127.0.0.1:8080/greeting?name=quanke.name
输出:
那么恭喜你,一个kotlin 和spring boot的web服务就这么搞定了。。。
服务默认是8080端口,如果8080端口刚好被占用了,那在resources目录下新建一个application.yml文件:
server:
port: 1234
把端口改成你想要的。
当然这只是开始的演示,么有什么卵用。。。之后可以用于生产的demo,呼之欲出。
《Spring Boot in kotlin 实战》,欢迎关注!
全科龙婷▼升职加薪
▼