In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.