如何使用kotlin编写spring cloud微服务

发布时间:2021-09-03 09:06:55 作者:小新
来源:亿速云 阅读:155

这篇文章主要为大家展示了“如何使用kotlin编写spring cloud微服务”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何使用kotlin编写spring cloud微服务”这篇文章吧。

创建工程

使用idea的spring initializr创建一个项目,语言选择kotlin, 类型为gradle。

如何使用kotlin编写spring cloud微服务

根据需要选择依赖

如何使用kotlin编写spring cloud微服务

配置文件

yml或者properties文件和java是完全一样的,这里不详细说明

修改build.gradle.kts中的参数:

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

/**
 * 商品服务
 */
@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)
    }

}

这是一个controller,通过构造函数注入依赖。

JPA

实体类:

@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>

由于没有自定义的方法,直接定义一个接口即可。

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对应的类是 org.junit.jupiter.api.Test

以上是“如何使用kotlin编写spring cloud微服务”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. 如何正确使用 Spring Cloud?【上】
  2. 如何正确使用 Spring Cloud?【下】

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

kotlin springcloud

上一篇:Linux系统下Shell多线程编程的实例用法

下一篇:MySQL中的隐藏列的具体查看方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》