使用Spring Boot和Kotlin创建RESTfull API

使用Kotlin 结合SpringBoot 开发一个RESTFul版本的 Hello World

如果单单Kotlin 我是不相信在短时间内有产生多大的风波,但是如果Kotlin​ 能和 Java 开发神器 SpringBoot 结合起来,我感觉能飞。。。

1.首先新建GradleKotlin工程

打开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.gradlesettings.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"

}
下载Gradle 依赖

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

从生成的配置文件看,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:

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 实战》,欢迎关注!

全科龙婷
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值