[kotcloud] kotlin + springboot (一)初始配置

[KotCloud] maven配置springboot + kotlin

pom.xml

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <kotlin.version>1.2.31</kotlin.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>
<dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jre8</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
<build>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <jvmTarget>${java.version}</jvmTarget>
                </configuration>
            </plugin>
        </plugins>
    </build>

配置maven結構目錄:

src -- 
    |--main
        |--java
            |--com.kotcloud
                |--controller
                    |--IndexController.kt
                |--KotCloudApp.kt
        |--resource
        |--webapp
    |--test
    .......
    .......
  • java目錄下 , 一定要有二級目錄, 否則springboot不會自動識別註解
  • App文件需要在子文件的最外層

App.kt

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication
open class KotCloudApp

fun main(args : Array<String>){
    SpringApplication.run(KotCloudApp::class.java,*args)
}
  • class必須是open的,即public的.

IndexController.kt

import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
open class IndexController{
    @RequestMapping("/index")
    fun index(id:Int?) : String{
        return id?.toString()?:"0"
    }
}
  • class必須是open的,即public的.
  • fun必須在class體內 , 不能寫在class外, 否則springboot識別不到註解

以下是錯誤的:

// 這是錯誤的方式  方法index不會被識別
@RestController
open class IndexController

@RequestMapping("/index")
fun index(id:Int?) : String{
    return id?.toString()?:"0"
}

啓動

啓動說明 啓動輸出

  1. 啓動服務的host , 及context (context默認根目錄: "/")
  2. 當前已經識別到的url地址
  3. 服務啓動的端口號: 8080 (默認使用tomcat服務,tomcat默認端口號爲:8080)

請求服務

  • localhost:8080/index
    • 0
  • localhost:8080/index?id=123
    • 123
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章