使用kotlin編寫spring cloud微服務(wù)的過程
創(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"
//自動依賴包版本管理
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)
}
這是自動生成程序入口,不用修改
編寫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對應(yīng)的類是
org.junit.jupiter.api.Test
到此這篇關(guān)于使用kotlin編寫spring cloud微服務(wù)的文章就介紹到這了,更多相關(guān)kotlin spring cloud微服務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用SpringJPA?直接實(shí)現(xiàn)count(*)
這篇文章主要介紹了SpringJPA?直接實(shí)現(xiàn)count(*),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
java實(shí)現(xiàn)二維碼生成的幾個(gè)方法(推薦)
本篇文章主要介紹了java實(shí)現(xiàn)二維碼生成的幾個(gè)方法(推薦),具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12
java中JSONObject轉(zhuǎn)換為HashMap(方法+main方法調(diào)用實(shí)例)
這篇文章主要介紹了java中JSONObject轉(zhuǎn)換為HashMap(方法+main方法調(diào)用實(shí)例),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
關(guān)于java中@Async異步調(diào)用詳細(xì)解析附代碼
本文主要介紹了java關(guān)于@Async異步調(diào)用詳細(xì)解析附代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
在RedisTemplate中使用scan代替keys指令操作
這篇文章主要介紹了在RedisTemplate中使用scan代替keys指令操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Java使用get請求接收List集合數(shù)據(jù)(json)并導(dǎo)出報(bào)表問題
這篇文章主要介紹了Java使用get請求接收List集合數(shù)據(jù)(json)并導(dǎo)出報(bào)表問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Spring boot搭建web應(yīng)用集成thymeleaf模板實(shí)現(xiàn)登陸
這篇文章主要介紹了Spring boot搭建web應(yīng)用集成thymeleaf模板實(shí)現(xiàn)登陸,頁面使用bootstrap,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
使用Java接收和處理OpenTelemetry數(shù)據(jù)的完整指南
在現(xiàn)代分布式系統(tǒng)中,OpenTelemetry 成為了一種常見的標(biāo)準(zhǔn),用于跟蹤和監(jiān)控應(yīng)用程序的性能和行為,OTLP是 OpenTelemetry 社區(qū)定義的一種數(shù)據(jù)傳輸協(xié)議,文將介紹如何使用 Java 編寫代碼來接收和處理 OTLP 數(shù)據(jù),需要的朋友可以參考下2024-04-04
IDEA與模擬器安裝調(diào)試失敗的處理方法:INSTALL_PARSE_FAILED_NO_CERTIFICATES
這篇文章主要介紹了IDEA與模擬器安裝調(diào)試失敗的處理方法:INSTALL_PARSE_FAILED_NO_CERTIFICATES,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
詳解SpringBoot2 使用Spring Session集群
這篇文章主要介紹了SpringBoot2 使用Spring Session集群,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-04-04

