基於gradle構建kotlin的springboot項目

章魚哥springboot筆記之:kotlin的springboot項目搭建

廢話不多說直接上代碼:https://start.spring.io/

選擇完後直接下載,idea導入

注意:導入的時候選擇gradle項目

build.gradle文件如下

buildscript {
	ext {
		kotlinVersion = '1.2.51'
		springBootVersion = '2.0.5.RELEASE'
	}
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
		classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
		classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
	}
}

apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.shuma'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
	kotlinOptions {
		freeCompilerArgs = ["-Xjsr305=strict"]
		jvmTarget = "1.8"
	}
}
compileTestKotlin {
	kotlinOptions {
		freeCompilerArgs = ["-Xjsr305=strict"]
		jvmTarget = "1.8"
	}
}

repositories {
	mavenLocal()
	mavenCentral()
}


dependencies {
	compile('org.springframework.boot:spring-boot-starter-web')
	compile('com.fasterxml.jackson.module:jackson-module-kotlin')
	compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
	compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
	compile("org.jetbrains.kotlin:kotlin-reflect")
	compile group: 'com.alibaba', name: 'druid', version: '1.1.3'
	runtime('mysql:mysql-connector-java')
	testCompile('org.springframework.boot:spring-boot-starter-test')
}

kotlin版本hello word!

package com.shuma.kotlin.controller

import com.shuma.kotlin.model.User
import com.shuma.kotlin.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class TestController {

    @Autowired
    lateinit var userService :UserService

    @GetMapping("/hello")
    fun test():String{
        return "hello kotlin web!"
    }
}

main方法啓動項目:http://127.0.0.1:8080/hello

親測可用,githup地址:https://github.com/tzj1042/springBoot-kotlin

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章