今天我们尝试spring boot整合kotlin,并决定建立一个非常简单的spring boot微服务,使用kotlin作为编程语言进行编码构建。
创建一个简单的spring boot应用程序。我会在这里使用maven构建项目:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
<?xml version= "1.0" encoding= "utf-8" ?>
<project xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns= "http://maven.apache.org/pom/4.0.0"
xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
<modelversion> 4.0 . 0 </modelversion>
<groupid>com.edurt.ski</groupid>
<artifactid>springboot-kotlin-integration</artifactid>
<version> 1.0 . 0 </version>
<packaging>jar</packaging>
<name>springboot kotlin integration</name>
<description>springboot kotlin integration is a open source springboot, kotlin integration example.</description>
<parent>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-parent</artifactid>
<version> 2.1 . 3 .release</version>
<relativepath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceencoding>utf- 8 </project.build.sourceencoding>
<project.reporting.outputencoding>utf- 8 </project.reporting.outputencoding>
<java.version> 1.8 </java.version>
<!-- plugin config -->
<plugin.maven.kotlin.version> 1.2 . 71 </plugin.maven.kotlin.version>
</properties>
<dependencies>
<!-- spring boot -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
<!-- kotlin -->
<dependency>
<groupid>com.fasterxml.jackson.module</groupid>
<artifactid>jackson-module-kotlin</artifactid>
</dependency>
<dependency>
<groupid>org.jetbrains.kotlin</groupid>
<artifactid>kotlin-stdlib-jdk8</artifactid>
</dependency>
<dependency>
<groupid>org.jetbrains.kotlin</groupid>
<artifactid>kotlin-reflect</artifactid>
</dependency>
</dependencies>
<build>
<sourcedirectory>${project.basedir}/src/main/kotlin</sourcedirectory>
<testsourcedirectory>${project.basedir}/src/test/kotlin</testsourcedirectory>
<plugins>
<plugin>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-maven-plugin</artifactid>
</plugin>
<plugin>
<artifactid>kotlin-maven-plugin</artifactid>
<groupid>org.jetbrains.kotlin</groupid>
<configuration>
<args>
<arg>-xjsr305=strict</arg>
</args>
<compilerplugins>
<plugin>spring</plugin>
<plugin>jpa</plugin>
<plugin>all-open</plugin>
</compilerplugins>
<pluginoptions>
<option>all-open:annotation=javax.persistence.entity</option>
</pluginoptions>
</configuration>
<dependencies>
<dependency>
<groupid>org.jetbrains.kotlin</groupid>
<artifactid>kotlin-maven-allopen</artifactid>
<version>${plugin.maven.kotlin.version}</version>
</dependency>
<dependency>
<groupid>org.jetbrains.kotlin</groupid>
<artifactid>kotlin-maven-noarg</artifactid>
<version>${plugin.maven.kotlin.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
<configuration>
<sourcedirs>
<sourcedir>src/main/kotlin</sourcedir>
</sourcedirs>
<annotationprocessorpaths>
<annotationprocessorpath>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-configuration-processor</artifactid>
<version>${project.parent.version}</version>
</annotationprocessorpath>
</annotationprocessorpaths>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
|
添加所有必需的依赖项:
- kotlin-stdlib-jdk8 kotlin jdk8的lib包
- kotlin-reflect kotlin反射包
一个简单的应用类:
1
2
3
4
5
6
7
8
9
10
11
|
package com.edurt.ski
import org.springframework.boot.autoconfigure.springbootapplication
import org.springframework.boot.runapplication
@springbootapplication
class springbootkotlinintegration
fun main(args: array<string>) {
runapplication<springbootkotlinintegration>(*args)
}
|
添加rest api接口功能
创建一个hellocontroller rest api接口,我们只提供一个简单的get请求获取hello,kotlin输出信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.edurt.ski.controller
import org.springframework.web.bind.annotation.getmapping
import org.springframework.web.bind.annotation.restcontroller
@restcontroller
class hellocontroller {
@getmapping (value = "hello" )
fun hello(): string {
return "hello,kotlin"
}
}
|
修改springbootkotlinintegration文件增加以下设置扫描路径
1
2
3
4
|
@componentscan (value = [
"com.edurt.ski" ,
"com.edurt.ski.controller"
])
|
添加页面功能
修改pom.xml文件增加以下页面依赖
1
2
3
4
5
|
<!-- mustache -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-mustache</artifactid>
</dependency>
|
在src/main/resources路径下创建templates文件夹
在templates文件夹下创建一个名为hello.mustache的页面文件
1
|
<h1>hello, kotlin</h1>
|
创建页面转换器helloview
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.edurt.ski.view
import org.springframework.stereotype.controller
import org.springframework.ui.model
import org.springframework.web.bind.annotation.getmapping
@controller
class helloview {
@getmapping (value = "hello_view" )
fun helloview(model: model): string {
return "hello"
}
}
|
浏览器访问http://localhost:8080/hello_view即可看到页面内容
添加数据持久化功能
修改pom.xml文件增加以下依赖(由于测试功能我们使用h2内存数据库)
1
2
3
4
5
6
7
8
9
10
|
<!-- data jpa and db -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-jpa</artifactid>
</dependency>
<dependency>
<groupid>com.h2database</groupid>
<artifactid>h2</artifactid>
<scope>runtime</scope>
</dependency>
|
创建user实体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package com.edurt.ski.model
import javax.persistence.entity
import javax.persistence.generatedvalue
import javax.persistence.id
@entity
//class usermodel(
// @id
// @generatedvalue
// private var id: long? = 0,
// private var name: string
//)
class usermodel {
@id
@generatedvalue
var id: long ? = 0
get() = field
set
var name: string? = null
get() = field
set
}
|
创建usersupport dao数据库操作工具类
1
2
3
4
5
6
7
8
|
package com.edurt.ski.support
import com.edurt.ski.model.usermodel
import org.springframework.data.repository.pagingandsortingrepository
interface usersupport : pagingandsortingrepository<usermodel, long > {
}
|
创建userservice服务类
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.edurt.ski.service
import com.edurt.ski.model.usermodel
interface userservice {
/**
* save model to db
*/
fun save(model: usermodel): usermodel
}
|
创建userserviceimpl实现类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.edurt.ski.service
import com.edurt.ski.model.usermodel
import com.edurt.ski.support.usersupport
import org.springframework.stereotype.service
@service (value = "userservice" )
class userserviceimpl( private val usersupport: usersupport) : userservice {
override fun save(model: usermodel): usermodel {
return this .usersupport.save(model)
}
}
|
创建用户usercontroller进行持久化数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.edurt.ski.controller
import com.edurt.ski.model.usermodel
import com.edurt.ski.service.userservice
import org.springframework.web.bind.annotation.pathvariable
import org.springframework.web.bind.annotation.postmapping
import org.springframework.web.bind.annotation.requestmapping
import org.springframework.web.bind.annotation.restcontroller
@restcontroller
@requestmapping (value = "user" )
class usercontroller( private val userservice: userservice) {
@postmapping (value = "save/{name}" )
fun save( @pathvariable name: string): usermodel {
val usermodel = usermodel()
// usermodel.id = 1
usermodel.name = name
return this .userservice.save(usermodel)
}
}
|
使用控制台窗口执行以下命令保存数据
1
|
curl -x post http: //localhost:8080/user/save/qianmoq
|
收到返回结果
{"id":1,"name":"qianmoq"}
表示数据保存成功
增加数据读取渲染功能
修改userservice增加以下代码
1
2
3
4
|
/**
* get all model
*/
fun getall(page: pageable): page<usermodel>
|
修改userserviceimpl增加以下代码
1
2
3
|
override fun getall(page: pageable): page<usermodel> {
return this .usersupport.findall(page)
}
|
修改usercontroller增加以下代码
1
2
|
@getmapping (value = "list" )
fun get(): page<usermodel> = this .userservice.getall(pagerequest( 0 , 10 ))
|
创建userview文件渲染user数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.edurt.ski.view
import com.edurt.ski.service.userservice
import org.springframework.data.domain.pagerequest
import org.springframework.stereotype.controller
import org.springframework.ui.model
import org.springframework.ui.set
import org.springframework.web.bind.annotation.getmapping
@controller
class userview( private val userservice: userservice) {
@getmapping (value = "user_view" )
fun helloview(model: model): string {
model[ "users" ] = this .userservice.getall(pagerequest( 0 , 10 ))
return "user"
}
}
|
创建user.mustache文件渲染数据(自行解析返回数据即可)
1
|
{{users}}
|
浏览器访问http://localhost:8080/user_view即可看到页面内容
增加单元功能
修改pom.xml文件增加以下依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<!-- test -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-test</artifactid>
<scope>test</scope>
<exclusions>
<exclusion>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
</exclusion>
<exclusion>
<groupid>org.mockito</groupid>
<artifactid>mockito-core</artifactid>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupid>org.junit.jupiter</groupid>
<artifactid>junit-jupiter-engine</artifactid>
<scope>test</scope>
</dependency>
|
创建userservicetest文件进行测试userservice功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.edurt.ski
import com.edurt.ski.service.userservice
import org.junit.jupiter.api.afterall
import org.junit.jupiter.api.test
import org.springframework.beans.factory.annotation.autowired
import org.springframework.boot.test.context.springboottest
import org.springframework.data.domain.pagerequest
@springboottest (webenvironment = springboottest.webenvironment.random_port)
class userservicetest( @autowired private val userservice: userservice) {
@test
fun `get all`() {
println( ">> assert blog page title, content and status code" )
val entity = this .userservice.getall(pagerequest( 0 , 1 ))
print(entity.totalpages)
}
}
|
源码地址:github
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://segmentfault.com/a/1190000018224145