1. 程式人生 > >基於gradle構建kotlin的springboot專案

基於gradle構建kotlin的springboot專案

章魚哥springboot筆記之:kotlin的springboot專案搭建

選擇完後直接下載,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