国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|JavaScript|易語(yǔ)言|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - SpringBoot整合Kotlin構(gòu)建Web服務(wù)的方法示例

SpringBoot整合Kotlin構(gòu)建Web服務(wù)的方法示例

2021-07-16 16:11qianmoQ Java教程

這篇文章主要介紹了SpringBoot整合Kotlin構(gòu)建Web服務(wù)的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

今天我們嘗試spring boot整合kotlin,并決定建立一個(gè)非常簡(jiǎn)單的spring boot微服務(wù),使用kotlin作為編程語(yǔ)言進(jìn)行編碼構(gòu)建。

創(chuàng)建一個(gè)簡(jiǎn)單的spring boot應(yīng)用程序。我會(huì)在這里使用maven構(gòu)建項(xiàng)目:

?
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>

添加所有必需的依賴項(xiàng):

  • kotlin-stdlib-jdk8 kotlin jdk8的lib包
  • kotlin-reflect kotlin反射包

一個(gè)簡(jiǎn)單的應(yīng)用類:

?
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接口功能

創(chuàng)建一個(gè)hellocontroller rest api接口,我們只提供一個(gè)簡(jiǎn)單的get請(qǐng)求獲取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文件增加以下設(shè)置掃描路徑

?
1
2
3
4
@componentscan(value = [
  "com.edurt.ski",
  "com.edurt.ski.controller"
])

添加頁(yè)面功能

修改pom.xml文件增加以下頁(yè)面依賴

?
1
2
3
4
5
<!-- mustache -->
<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-mustache</artifactid>
</dependency>

在src/main/resources路徑下創(chuàng)建templates文件夾

在templates文件夾下創(chuàng)建一個(gè)名為hello.mustache的頁(yè)面文件

?
1
<h1>hello, kotlin</h1>

創(chuàng)建頁(yè)面轉(zhuǎn)換器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"
  }
 
}

瀏覽器訪問(wèn)http://localhost:8080/hello_view即可看到頁(yè)面內(nèi)容

添加數(shù)據(jù)持久化功能

修改pom.xml文件增加以下依賴(由于測(cè)試功能我們使用h2內(nèi)存數(shù)據(jù)庫(kù))

?
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>

創(chuàng)建user實(shí)體

?
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
 
}

創(chuàng)建usersupport dao數(shù)據(jù)庫(kù)操作工具類

?
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> {
 
}

創(chuàng)建userservice服務(wù)類

?
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
 
}

創(chuàng)建userserviceimpl實(shí)現(xiàn)類

?
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)
  }
 
}

創(chuàng)建用戶usercontroller進(jìn)行持久化數(shù)據(jù)

?
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)
  }
 
}

使用控制臺(tái)窗口執(zhí)行以下命令保存數(shù)據(jù)

?
1
curl -x post http://localhost:8080/user/save/qianmoq

收到返回結(jié)果

{"id":1,"name":"qianmoq"}

表示數(shù)據(jù)保存成功

增加數(shù)據(jù)讀取渲染功能

修改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))

創(chuàng)建userview文件渲染user數(shù)據(jù)

?
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"
  }
 
}

創(chuàng)建user.mustache文件渲染數(shù)據(jù)(自行解析返回?cái)?shù)據(jù)即可)

?
1
{{users}}

瀏覽器訪問(wèn)http://localhost:8080/user_view即可看到頁(yè)面內(nèi)容

增加單元功能

修改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>

創(chuàng)建userservicetest文件進(jìn)行測(cè)試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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://segmentfault.com/a/1190000018224145

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久久精品网 | 亚洲国产中文字幕 | 日韩精品免费视频 | 欧美视频一区二区 | 一区二区三区在线看 | 天天玩天天操天天射 | 成人午夜在线 | 国产精品久久久亚洲 | 九色自拍| 黄色毛片在线 | 国产精品一区二区久久 | 看av片 | 欧美专区在线观看 | 欧美成人精品一区二区男人看 | a级在线免费视频 | 日韩在线中文字幕 | 日本久久精品视频 | 无码日韩精品一区二区免费 | 国产日韩精品一区二区 | 欧美一区二区视频免费观看 | 久久久久久亚洲精品视频 | 99在线免费观看 | 免费激情网站 | 日韩中文字幕视频在线观看 | 最新国产精品精品视频 | 久久精品亚洲精品 | 一级国产免费 | a视频在线免费观看 | 日韩成人在线观看 | 成av人片在线观看www | 操操操av | 精品国产欧美一区二区 | 成人午夜精品一区二区三区 | 凹凸日日摸日日碰夜夜爽孕妇 | 国产麻豆乱码精品一区二区三区 | 欧美一区二区三区四区不卡 | 国产成人一区 | 国产在线拍揄自揄拍视频 | 最近免费中文字幕大全免费版视频 | a级在线免费视频 | 精品国产一区二区三区高潮视 |