Kotlin+SpringBoot服務端restful框架搭建(1)

本文的組要目的是完成SpringBoot集成Kotlin的一個基本項目構建,經過這個例子可以對kotlin有一個簡單的瞭解。與其看着枯燥的教程,看看它的使用也許會更有意思。java

kotlin簡介

Kotlin是一個基於JVM的編程語言,由JetBrains設計開發並開源。Kotlin能夠編譯成Java字節碼也能夠編譯成JavaScript,支持開發Android,而且谷歌已經明確將Kotlin做爲Android的一級開發語言。最新的Spring5版本也專門引入了對於Kotlin的支持。Kotlin擁有更簡潔的代碼,徹底兼容java,並且同java能夠實現徹底的互調。使用Kotlin你起碼能夠少些30%的代碼。web

要求

  • JDK 1.8
  • maven構建
  • Spring4
  • IDEA Intellij
  • Kotlin 1.2.31

快速構建

首先構建一個maven項目,咱們命名爲KotlinDemo。(使用idea構建Kotlin項目須要按照Kotlin的插件。)spring

1.先來修改pom.xml添加須要使用的依賴。 引入spring-boot-starter-parent,這裏使用1.5.6版本編程

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
</parent>

添加須要的依賴數組

<properties>
    <java.version>1.8</java.version>
    <mybatis.spring.version>1.2.4</mybatis.spring.version>
    <kotlin.compiler.incremental>true</kotlin.compiler.incremental>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <kotlin.version>1.2.31</kotlin.version>
  </properties>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib-jre8</artifactId>
    <version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-reflect</artifactId>
    <version>${kotlin.version}</version>
</dependency>

在build中添加插件restful

<plugin>
    <artifactId>kotlin-maven-plugin</artifactId>
    <groupId>org.jetbrains.kotlin</groupId>
    <version>1.2.31</version>
    <configuration>
        <compilerPlugins>
            <plugin>spring</plugin>
        </compilerPlugins>
        <jvmTarget>1.8</jvmTarget>
    </configuration>
        <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>
            <dependencies>
                <dependency>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-allopen</artifactId>
                    <version>1.2.31</version>
                    </dependency>
            </dependencies>
   </plugin>
  1. 建立application.yml
server:
  context-path: /kotlin
  port: 8090
  1. 建立Application.kt文件

建立類的方法同java類似,也是引入各類包,可是須要注意Kotlin的main方法是class外面和java的main方法區別mybatis

添加spring的註解@RestController和@SpringBootApplication,咱們在class裏面建立一個簡單的restfull格式的請求方法hello(),方法返一個String的字符串。app

添加@RequestMapping時候須要注意設置method的時候,這裏method接收的是一個數組,不是RequestMethod.GE,因此咱們須要經過kotlin提供的arrayOf()方法轉換一個下,這是一個建立數據的方法,具體的能夠參考一下kotlin的文檔。jvm

注意一下 SpringApplication.run中參數這個和java的區別有點大maven

Application.kt類代碼

package demo

import org.springframework.boot.SpringApplication
@RestController
@SpringBootApplication
class Application{

 @RequestMapping(value = "/hello",method =arrayOf(RequestMethod.GET))
    fun hello(): String {

        return "hello kotlin";

    }
}

fun main(args: Array<String>){

   //::符號是獲得這個類的class對象
    SpringApplication.run(Application::class.java,*args);

}
  1. 項目基本搭建完成,試試啓動項目訪問http://localhost:8090/demo/hello,頁面會返回hello kotlin。

輸入圖片說明

相關文章
相關標籤/搜索