使用 Kotlin 结合 SpringBoot 开发一个 RESTFul 版本的 Hello World.
如果单单 Kotlin 我是不相信在短时间内有产生多大的风波,但是如果 Kotlin 能和 Java 开发神器 SpringBoot 结合起来,我感觉能飞...
1. 首先新建 Gradle 的 Kotlin 工程
打开 IDEA ,File->New->Project
New->Project
输入相关信息之后下一步:
一般 GroupId 为公司域名反着写,加上项目名字
image
建议选中 Use auto-import,自动导包 , 使用本地 Gradle,但是需要先安装 Gradle( https://gradle.org/install/ )
Use auto-import
如果没有选择 使用本地 Gradle 项目创建完成之后 Gradle 的包需要下载,这个时间有点长,以前做 Android 就深受其苦,所有要做好心理准备,除非你有一个好用的 FQ 工具.
Gradle 库下载中
项目创建完成之后会生成一个 Gradle 文件 build.gradle 和 settings.gradle,settings.gradle 文件我们暂且不管,先看看 build.gradle 文件:
下载 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 文件
3. 创建包
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"
}
在 kotlin 目录下面创建:
name.quanke.kotlin.rest
注意:不能直接在 kotlin 目录下面写 Application 类,不然会报错
Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package
3. 创建数据模型 Greeting 类:
如果是 Java 写的话,想象一下这需要多少行代码,看看 kotlin,不明则厉...
package name.quanke.kotlin.rest
/**
* Created by http://quanke.name on 2018/1/9.
*/
data class Greeting(val id: Long, val content: String)
5. 创建 GreetingController
6. 创建 Application
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")
}
点击 Gradle 的 bootRun:
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)
}
bootRun
或者点击
Run
运行成功的样子
如果没有毛病,访问
http://127.0.0.1:8080/greeting?name=quanke.name
输出:
image
那么恭喜你,一个 kotlin 和 spring boot 的 web 服务就这么搞定了...
服务默认是 8080 端口,如果 8080 端口刚好被占用了,那在 resources 目录下新建一个 application.yml 文件:
把端口改成你想要的.
server:
port: 1234
当然这只是开始的演示,么有什么卵用...之后可以用于生产的 demo,呼之欲出.
《Spring Boot in kotlin 实战》,欢迎关注!
来源: http://www.jianshu.com/p/56f81ab8ee45