Spring Boot Kotlin Web开发 CRUD

整个操作过程的视频已经上传了。
youku视频
youtub视频

项目地址:https://github.com/aslucky/springBoot-kotlin-crud.git

建立项目

使用IDEA开发Spring Boot + Kotlin搭建后端Restful服务
Java 1.8
File-New project- Spring initializr下一步,语言选择Kotlin,其他根据需要选择,这里使用maven。
下一步,依赖里面选择Web-Web其他的暂时不选,下一步设置项目名称,完毕。
大致如下:

30f395b9.png
DemoApplication.kt文件里面是main入口函数。

添加控制器代码

新建一个Kotlin类controllerTopic

@RestController
class controllerTopic {

@RequestMapping(value = "/",method = arrayOf(RequestMethod.GET))
fun index():String = "hi there"

}

在DemoApplication.kt文件上右键-debug…一般情况下会成功启动服务
使用Postman发送请求Get http://localhost:8080/
会看到回复hi there

实现CRUD接口

添加一个数据类Topic,设置了默认值。

data class Topic (val id:String="", val name:String="", val description:String="") {
}

添加一个服务类TopicService,实现CRUD服务,这里暂时先使用一个可写列表作为数据源。

@Service
class TopicService {

private var topics = mutableListOf(
Topic("spring", "spring framework", "spring description"),
Topic("java", "java framework", "java description"),
Topic("java script", "java script framework", "java script description")
)

fun getAll(): List<Topic> = topics

fun getTopic(id: String) = topics.filter({ it -> it.id == id }).firstOrNull()

// return Boolean. true success otherwise failed
fun addTopic(topic: Topic) = topics.add(topic)

fun updateTopic(id: String, topic: Topic):Int {
var list = topics.filter({ it -> it.id == id }).firstOrNull()
if (list == null)
return -1
else
topics.set(topics.indexOf(list), topic)
return 0
}

//true : success otherwise false
fun deleteTopic(id: String): Boolean{
val ret = topics.removeIf({ it -> it.id.equals(id) })
return ret
}
}

创建一个用来返回信息的数据类ProcResp

data class procResp(var code:Int, var message:String,var data:Any? = null)

处理控制器的操作

@RestController
class controllerTopic(val topicService: TopicService) {

@RequestMapping(value = "/",method = arrayOf(RequestMethod.GET))
fun index():String = "hi there"

@RequestMapping(value = "/topics",method = arrayOf(RequestMethod.GET))
fun getAllTopic(): procResp{
var resp = procResp(0,"success")
resp.data = topicService.getAll()
return resp
}

@RequestMapping(value = "topics/{id}",method = arrayOf(RequestMethod.GET))
fun getTopic(@PathVariable id: String) : procResp
{
var resp = procResp(0,"success")
resp.data = topicService.getTopic(id)
return resp
}

@RequestMapping(value = "topics",method = arrayOf(RequestMethod.POST))
fun addTopic(@RequestBody topic:Topic):procResp{
var resp = procResp(if (topicService.addTopic(topic)) 0 else -1,"success")
return resp
}

@RequestMapping(value = "topics/{id}",method = arrayOf(RequestMethod.PUT))
fun updateTopic(@RequestBody topic:Topic, @PathVariable id:String):procResp{
val ret = topicService.updateTopic(id, topic)
var resp = procResp(ret,if (ret != 0) "error" else "success")
return resp
}

@RequestMapping(value = "topics/{id}",method = arrayOf(RequestMethod.DELETE))
fun deleteTopic(@PathVariable id: String) : procResp{
val ret = if (topicService.deleteTopic(id)) 0 else -1
var resp = procResp(ret ,if (ret != 0) "error" else "success")
return resp
}
}

现在就可以运行起来了。照比我之前用spring mvc写的CRUD简单的太多了。
和python的flask很像。能自动处理的部分都屏蔽掉了,比较人性化。