使用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"
//自動(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對應(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)文章
Java 在Excel單元格中應(yīng)用一種/多種字體樣式(實(shí)例代碼)
這篇文章主要介紹了Java 在Excel單元格中應(yīng)用一種/多種字體樣式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
Java并發(fā)容器ConcurrentLinkedQueue解析
這篇文章主要介紹了Java并發(fā)容器ConcurrentLinkedQueue解析,2023-12-12
編寫調(diào)用新浪微博API的Java程序來發(fā)送微博
這篇文章主要介紹了編寫調(diào)用新浪微博API的Java程序來發(fā)送微博的方法,只是展示了一個(gè)基本的程序框架而非一個(gè)完整的圖形化軟件:)需要的朋友可以參考下2015-11-11
一文搞懂Java?ScheduledExecutorService的使用
JUC包(java.util.concurrent)中提供了對定時(shí)任務(wù)的支持,即ScheduledExecutorService接口。本文主要對ScheduledExecutorService的使用進(jìn)行簡單的介紹,需要的可以參考一下2022-11-11
Java中的ThreadPoolExecutor線程池原理細(xì)節(jié)解析
這篇文章主要介紹了Java中的ThreadPoolExecutor線程池原理細(xì)節(jié)解析,ThreadPoolExecutor是一個(gè)線程池,最多可使用7個(gè)參數(shù)來控制線程池的生成,使用線程池可以避免創(chuàng)建和銷毀線程的資源損耗,提高響應(yīng)速度,并且可以管理線程池中線程的數(shù)量和狀態(tài)等等,需要的朋友可以參考下2023-12-12
SpringBoot+Hutool實(shí)現(xiàn)圖片驗(yàn)證碼的示例代碼
圖片驗(yàn)證碼在注冊、登錄、交易、交互等各類場景中都發(fā)揮著巨大作用,本文主要介紹了SpringBoot+Hutool實(shí)現(xiàn)圖片驗(yàn)證碼的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01

