问题:
spring boot + kotlin + gradle demo
解决:
执行
然后访问 http://localhost:8080/hi?name=tom
代码:
.\build.gradle:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
buildscript { ext.kotlin_version = '1.2.0' repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } apply plugin: "kotlin" apply plugin: "application" mainClassName = "demo.App" sourceSets { deploy } repositories { maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'} mavenCentral() } dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile("org.springframework.boot:spring-boot-starter-web:1.5.10.RELEASE") testCompile 'junit:junit:4.11' testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version" } test.testClassesDir = project.tasks.compileTestKotlin.destinationDir task wrapper(type: Wrapper) { gradleVersion="4.1" } |
.\src\main\kotlin\App.kt
|
package demo import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; @SpringBootApplication open class App{ companion object { @JvmStatic fun main(argv:Array<String>){ SpringApplication.run(App::class.java); } } } |
.\src\main\kotlin\HiController.kt
|
package demo import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; //@SpringBootApplication @RestController class HiController { @RequestMapping("/hi") fun hi(name:String) : String{ return "hi ${name}" } } |
参考: