欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用kotlin編寫spring cloud微服務(wù)的過程

 更新時(shí)間:2021年09月02日 16:21:54   作者:xiegwei  
這篇文章主要介紹了使用kotlin編寫spring cloud微服務(wù)的相關(guān)知識(shí),本文給大家提到配置文件的操作代碼,給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

創(chuàng)建工程

使用idea的spring initializr創(chuàng)建一個(gè)項(xiàng)目,語言選擇kotlin, 類型為gradle。

在這里插入圖片描述

根據(jù)需要選擇依賴

在這里插入圖片描述

配置文件

yml或者properties文件和java是完全一樣的,這里不詳細(xì)說明

修改build.gradle.kts中的參數(shù):

plugins {
	//spring boot版本
	id("org.springframework.boot") version "2.3.3.RELEASE"
	//自動(dòng)依賴包版本管理
	id("io.spring.dependency-management") version "1.0.10.RELEASE"
	...
}
//spring cloud 版本
extra["springCloudVersion"] = "Hoxton.SR8"

repositories {
    //本地maven
	maven {
		url = uri("http://192.168.1.150:8081/repository/maven-public/")
		credentials {
			username = "admin"
			password = "admin"
		}
	}
	maven { url = uri("https://repo.spring.io/milestone") }
	jcenter {
		content {
			// just allow to include kotlinx projects
			// detekt needs 'kotlinx-html' for the html report
			includeGroup("org.jetbrains.kotlinx")
		}
	}
}
...

Application

/**
 * 商品服務(wù)
 */
@SpringBootApplication
class ProductApplication

/**
 * 程序入口
 */
fun main(args: Array<String>) {
	runApplication<ProductApplication>(*args)
}

這是自動(dòng)生成程序入口,不用修改

編寫controller

@RestController
@RequestMapping("v2/test")
class SpuManagerController(val xService: XService) {

    @PostMapping("")
    fun addSpu(@RequestBody addXxVO: AddXxVO):Long{
        return xrService.addX(addXxVO)
    }

}

這是一個(gè)controller,通過構(gòu)造函數(shù)注入依賴。

JPA

實(shí)體類:

@Entity(name = "table_name")
@DynamicInsert //不插入null
@DynamicUpdate
class XxPO(
            var code:String,
            var name:String,
            var createDate:Date?=null,
            var updatedDate: Date?=null,
            @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var id:Long?=null)

Repository:

interface XxRepository :CrudRepository<SpuPO,Long>

由于沒有自定義的方法,直接定義一個(gè)接口即可。

Service

單元測試

@SpringBootTest
@AutoConfigureMockMvc
@Transactional
class SpuManagerControllerTests @Autowired constructor(val mockMvc: MockMvc,
                                                       val xxRepository : XxRepository ) {
    @Test
    fun testAddSpu() {
        val vo= AddXxVO("test_code", "test_name")
        mockMvc.perform(
                MockMvcRequestBuilders.post("/v2/test")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(JSON.toJSONString(vo))
        ).andExpect {
            status().is2xxSuccessful
        }
        .andReturn()
        .response
        .contentAsString
        .apply {
            val id = this.toLong()
            val result = xxRepository .findById(id)
            assert(result.isPresent)

        }

    }
}

注意 @Test對(duì)應(yīng)的類是 org.junit.jupiter.api.Test

到此這篇關(guān)于使用kotlin編寫spring cloud微服務(wù)的文章就介紹到這了,更多相關(guān)kotlin spring cloud微服務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論