Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use kotlin to write spring cloud Micro Service

Shulou Source: shulou.com Published: 2022-06-03 19:36:48 09月10日 Update

This article mainly shows you "how to use kotlin to write spring cloud micro-services", the content is easy to understand, clear, hope to help you solve your doubts, let the editor lead you to study and learn "how to use kotlin to write spring cloud micro-services" this article.

Create a project

Use idea's spring initializr to create a project with a language of kotlin and a type of gradle.

Choose dependencies as needed

Configuration file

Yml or properties files are exactly the same as java, which are not specified here

Modify the parameters in build.gradle.kts:

Plugins {/ / spring boot version id ("org.springframework.boot") version "2.3.3.RELEASE" / / automatic dependency package version management id ("io.spring.dependency-management") version "1.0.10.RELEASE".} / / spring cloud version extra ["springCloudVersion"] = "Hoxton.SR8" repositories {/ / Local 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

/ * goods and services * / @ SpringBootApplicationclass ProductApplication/** * Program entry * / fun main (args: Array) {runApplication (* args)}

This is the automatically generated program entry and does not need to be modified.

Write controller

@ RestController@RequestMapping ("v2/test") class SpuManagerController (val xService: XService) {@ PostMapping (") fun addSpu (@ RequestBody addXxVO: AddXxVO): Long {return xrService.addX (addXxVO)}}

This is a controller that injects dependencies through the constructor.

JPA

Entity class:

@ Entity (name = "table_name") @ DynamicInsert / / do not insert null@DynamicUpdateclass 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

Since there is no custom method, you can define an interface directly.

Service

Slightly

Unit testing

@ SpringBootTest@AutoConfigureMockMvc@Transactionalclass 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} .andRetur n () .response. ContentAsString .apply {val id = this.toLong () val result = xxRepository .findById (id) assert (result.isPresent)}

Note that the class corresponding to @ Test is org.junit.jupiter.api.Test

The above is all the content of the article "how to write spring cloud Micro Services with kotlin". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Services content versions articles entries files programs this is learning help selection no functions units parameters goods entities projects interfaces methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Technology OPPO Reno Microsoft MariaDB