真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

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

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

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),順河企業(yè)網(wǎng)站建設(shè),順河品牌網(wǎng)站建設(shè),網(wǎng)站定制,順河網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,順河網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

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

<?xml version="1.0" encoding="UTF-8"?>


  4.0.0

  com.edurt.ski
  springboot-kotlin-integration
  1.0.0
  jar

  springboot kotlin integration
  SpringBoot Kotlin Integration is a open source springboot, kotlin integration example.

  
    org.springframework.boot
    spring-boot-starter-parent
    2.1.3.RELEASE
     
  

  
    UTF-8
    UTF-8
    1.8
    
    1.2.71
  

  
    
    
      org.springframework.boot
      spring-boot-starter-web
    
    
    
      com.fasterxml.jackson.module
      jackson-module-kotlin
    
    
      org.jetbrains.kotlin
      kotlin-stdlib-jdk8
    
    
      org.jetbrains.kotlin
      kotlin-reflect
    
  

  
    ${project.basedir}/src/main/kotlin
    ${project.basedir}/src/test/kotlin
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
      
        kotlin-maven-plugin
        org.jetbrains.kotlin
        
          
            -Xjsr305=strict
          
          
            spring
            jpa
            all-open
          
          
            
          
        
        
          
            org.jetbrains.kotlin
            kotlin-maven-allopen
            ${plugin.maven.kotlin.version}
          
          
            org.jetbrains.kotlin
            kotlin-maven-noarg
            ${plugin.maven.kotlin.version}
          
        
        
          
            kapt
            
              kapt
            
            
              
                src/main/kotlin
              
              
                
                  org.springframework.boot
                  spring-boot-configuration-processor
                  ${project.parent.version}
                
              
            
          
        
      
    
  

添加所有必需的依賴項(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) {
  runApplication(*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文件增加以下頁面依賴



  org.springframework.boot
  spring-boot-starter-mustache

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

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

Hello, Kotlin

創(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文件增加以下依賴(由于測試功能我們使用h3內(nèi)存數(shù)據(jù)庫)



  org.springframework.boot
  spring-boot-starter-data-jpa


  com.h3database
  h3
  runtime

創(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 {

}

創(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

修改UserServiceImpl增加以下代碼

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

修改UserController增加以下代碼

@GetMapping(value = "list")
fun get(): Page = 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文件增加以下依賴



  org.springframework.boot
  spring-boot-starter-test
  test
  
    
      junit
      junit
    
    
      org.mockito
      mockito-core
    
  


  org.junit.jupiter
  junit-jupiter-engine
  test

創(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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


網(wǎng)站題目:SpringBoot整合Kotlin構(gòu)建Web服務(wù)的方法示例
網(wǎng)站地址:http://weahome.cn/article/jeepic.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部