最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2019年02月21日 09:49:58   作者:qianmoQ  
這篇文章主要介紹了SpringBoot整合Kotlin構(gòu)建Web服務(wù)的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

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

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

<?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è)簡單的應(yīng)用類:

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è)簡單的get請求獲取hello,kotlin輸出信息:

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è)置掃描路徑

@ComponentScan(value = [
  "com.edurt.ski",
  "com.edurt.ski.controller"
])

添加頁面功能

修改pom.xml文件增加以下頁面依賴

<!-- 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的頁面文件

<h1>Hello, Kotlin</h1>

創(chuàng)建頁面轉(zhuǎn)換器HelloView

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即可看到頁面內(nèi)容

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

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

<!-- 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í)體

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ù)庫操作工具類

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ù)類

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)類

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ù)

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ù)

curl -X POST http://localhost:8080/user/save/qianmoQ

收到返回結(jié)果

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

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

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

修改UserService增加以下代碼

/**
 * get all model
 */
fun getAll(page: Pageable): Page<UserModel>

修改UserServiceImpl增加以下代碼

override fun getAll(page: Pageable): Page<UserModel> {
  return this.userSupport.findAll(page)
}

修改UserController增加以下代碼

@GetMapping(value = "list")
fun get(): Page<UserModel> = this.userService.getAll(PageRequest(0, 10))

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

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ù)即可)

{{users}}

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

增加單元功能

修改pom.xml文件增加以下依賴

<!-- 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)行測試UserService功能

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java synchronized的用法及原理詳解

    java synchronized的用法及原理詳解

    如果要保證并發(fā)情況下多線程共享數(shù)據(jù)的訪問安全,操作的原子性,就可以使用synchronized關(guān)鍵字。這篇文章主要介紹了java synchronized的用法及原理,需要的朋友可以借鑒一下
    2021-08-08
  • MyBatis自定義resultMap三種映射關(guān)系示例詳解

    MyBatis自定義resultMap三種映射關(guān)系示例詳解

    這篇文章主要介紹了MyBatis自定義resultMap三種映射關(guān)系,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • Java 使用maven實(shí)現(xiàn)Jsoup簡單爬蟲案例詳解

    Java 使用maven實(shí)現(xiàn)Jsoup簡單爬蟲案例詳解

    這篇文章主要介紹了Java 使用maven實(shí)現(xiàn)Jsoup簡單爬蟲案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • Spring MVC 處理Ajax請求的方式詳解

    Spring MVC 處理Ajax請求的方式詳解

    本文介紹了在SpringMVC中處理Ajax請求的方法,主要依賴于Controller和返回類型的配置,使用@RequestMapping注解處理Ajax的控制器方法,本文給大家介紹Spring MVC 處理Ajax請求的方式,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • IDEA去除xml文件的黃色背景的操作步驟

    IDEA去除xml文件的黃色背景的操作步驟

    這篇文章主要介紹了IDEA去除xml文件的黃色背景的方法,本文通過圖文結(jié)合的方式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考價(jià)值,感興趣的朋友可以參考下
    2023-12-12
  • java多線程中的異常處理機(jī)制簡析

    java多線程中的異常處理機(jī)制簡析

    在java多線程程序中,所有線程都不允許拋出未捕獲的checked exception,也就是說各個(gè)線程需要自己把自己的checked exception處理掉,需要了解的朋友可以參考下
    2012-11-11
  • Java 實(shí)戰(zhàn)范例之員工管理系統(tǒng)的實(shí)現(xiàn)

    Java 實(shí)戰(zhàn)范例之員工管理系統(tǒng)的實(shí)現(xiàn)

    讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+vue+Springboot+ssm+mysql+maven+redis實(shí)現(xiàn)一個(gè)前后端分離的員工管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平
    2021-11-11
  • Java代碼規(guī)范與質(zhì)量檢測插件SonarLint的使用

    Java代碼規(guī)范與質(zhì)量檢測插件SonarLint的使用

    本文主要介紹了Java代碼規(guī)范與質(zhì)量檢測插件SonarLint的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Java遍歷Map的5種方式實(shí)例

    Java遍歷Map的5種方式實(shí)例

    Map作為Java中的一種集合,以鍵值對的形式存放一批數(shù)據(jù),經(jīng)常會(huì)被我們應(yīng)用在項(xiàng)目中,下面這篇文章主要給大家介紹了關(guān)于Java遍歷Map的5種方式,需要的朋友可以參考下
    2023-02-02
  • 如何配置Eclipse實(shí)現(xiàn)定制登錄界面

    如何配置Eclipse實(shí)現(xiàn)定制登錄界面

    本文介紹了如何配置Eclipse實(shí)現(xiàn)定制登錄界面,每一步的講解都很細(xì)致,感興趣的小伙伴可以閱讀一下
    2015-07-07

最新評論

鹤壁市| 普格县| 阳东县| 厦门市| 江华| 南汇区| 扶沟县| 黄平县| 轮台县| 门源| 类乌齐县| 黎城县| 墨脱县| 铁岭县| 玉溪市| 萝北县| 健康| 新宁县| 丘北县| 乐清市| 昆明市| 容城县| 盐山县| 肇东市| 青州市| 蒙阴县| 乌苏市| 栾川县| 佛教| 谷城县| 荆门市| 滨海县| 湘潭县| 河南省| 历史| 错那县| 宾阳县| 察雅县| 大悟县| 长宁区| 宾川县|