使用Spring Boot和Kotlin创建RESTfull API

1,171 阅读3分钟
原文链接: mp.weixin.qq.com

使用 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.gradlesettings.gradle, settings.gradle文件我们暂且不管,先看看 build.gradle文件:

                                                    
  1. group 'name.quanke.kotlin.restful'

  2. version '1.0-SNAPSHOT'

  3. buildscript {

  4. ext .kotlin_version = '1.2.10'

  5. repositories {

  6. mavenCentral ()

  7. }

  8. dependencies {

  9. classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

  10. }

  11. }

  12. apply plugin : 'kotlin'

  13. repositories {

  14. mavenCentral ()

  15. }

  16. dependencies {

  17. compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"

  18. }

  19. compileKotlin {

  20. kotlinOptions .jvmTarget = "1.8"

  21. }

  22. compileTestKotlin {

  23. kotlinOptions .jvmTarget = "1.8"

  24. }

下载依赖和插件也是一个漫长痛苦的过程。各位不要心急,慢慢来,一次不行多试几次。

从生成的配置文件看, IDEA已经自动把 Gradle构建 Kotlin工程插件 kotlin-gradle-plugin,以及 Kotlin

标准库 kotlin-stdlib添加到 build.gradle文件中了。

2.配置 build.gradle文件

                                                                    
  1. group 'name.quanke.kotlin.rest'

  2. version '1.0-SNAPSHOT'

  3. buildscript {

  4. ext .kotlin_version = '1.2.10'

  5. ext .spring_boot_version = '1.5.4.RELEASE'

  6. repositories {

  7. mavenCentral ()

  8. }

  9. dependencies {

  10. classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

  11. classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin

  12. // org.jetbrains.kotlin:kotlin-allopen 是全开放编译器插件。我们使用Kotlin 调用Java的Spring AOP框架和库,需要类为 open(可被继承实现),而Kotlin 类和函数都是默认 final 的,这样我们需要为每个类和函数前面加上open修饰符。

  13. // 这样的代码写起来,可费事了。还好,我们有all-open 编译器插件。它会适配 Kotlin 以满足这些框架的需求,并使用指定的注解标注类而其成员无需显式使用 open 关键字打开。 例如,当我们使用 Spring 时,就不需要打开所有的类,跟我们在Java中写代码一样,只需要用相应的注解标注即可。

  14. classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"

  15. }

  16. }

  17. apply plugin : 'kotlin'

  18. apply plugin : "kotlin-spring" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin

  19. apply plugin : 'org.springframework.boot'

  20. jar {

  21. baseName = '0_2RestfulApi-service'

  22. version = '0.1.0'

  23. }

  24. repositories {

  25. mavenCentral ()

  26. }

  27. dependencies {

  28. compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"

  29. compile 'org.springframework.boot:spring-boot-starter-web'

  30. testCompile ('org.springframework.boot:spring-boot-starter-test' )

  31. }

  32. compileKotlin {

  33. kotlinOptions .jvmTarget = "1.8"

  34. }

  35. compileTestKotlin {

  36. kotlinOptions .jvmTarget = "1.8"

  37. }

3.创建包

在kotlin目录下面创建:

  1. name.quanke.kotlin.rest

注意:不能直接在kotlin目录下面写Application 类,不然会报错

  1. Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package

3.创建数据模型Greeting类:

  1. package name.quanke.kotlin.rest

  2. /**

  3. * Created by http://quanke.name on 2018/1/9.

  4. */

  5. data class Greeting(val id: Long, val content: String)

如果是Java写的话,想象一下这需要多少行代码,看看kotlin,不明则厉。。。

5.创建GreetingController

  1. package name.quanke.kotlin.rest

  2. import org.springframework.web.bind.annotation.GetMapping

  3. import org.springframework.web.bind.annotation.RequestParam

  4. import org.springframework.web.bind.annotation.RestController

  5. import java.util.concurrent.atomic.AtomicLong

  6. /**

  7. * Created by http://quanke.name on 2018/1/9.

  8. */

  9. @RestController

  10. class GreetingController {

  11. val counter = AtomicLong()

  12. @GetMapping("/greeting")

  13. fun greeting(@RequestParam(value = "name", defaultValue = "World") name: String) =

  14. Greeting(counter.incrementAndGet(), "Hello, $name")

  15. }

6.创建 Application

                                                                        
  1. package name. quanke. kotlin. rest

  2. import org. springframework. boot. SpringApplication

  3. import org. springframework. boot. autoconfigure. SpringBootApplication

  4. /**

  5. * Created by http://quanke.name on 2018/1/9.

  6. */

  7. @SpringBootApplication

  8. class Application

  9. fun main (args : Array< String>) {

  10. SpringApplication .run (Application ::class .java , *args )

  11. }

点击Gradle的bootRun:

或者点击

如果没有毛病,访问

http://127.0.0.1:8080/greeting?name=quanke.name

输出:

那么恭喜你,一个kotlin 和spring boot的web服务就这么搞定了。。。

服务默认是8080端口,如果8080端口刚好被占用了,那在resources目录下新建一个application.yml文件:

  1. server:

  2.  port: 1234

把端口改成你想要的。

当然这只是开始的演示,么有什么卵用。。。之后可以用于生产的demo,呼之欲出。

《Spring Boot in kotlin 实战》,欢迎关注!


全科龙婷▼升职加薪