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

   本文的组要目的是完成SpringBoot集成Kotlin的一个基本项目构建,通过这个例子能够对kotlin有一个简单的了解。与其看着枯燥的教程,看看它的使用也许会更有意思。

kotlin简介
      Kotlin是一个基于JVM的编程语言,由JetBrains设计开发并开源。Kotlin可以编译成Java字节码也可以编译成JavaScript,支持开发Android,并且谷歌已经明确将Kotlin作为Android的一级开发语言。最新的Spring5版本也专门引入了对于Kotlin的支持。Kotlin拥有更简洁的代码,完全兼容java,而且同java可以实现完全的互调。使用Kotlin你起码可以少些30%的代码
 
要求
  • JDK 1.8
  • maven构建
  • Spring4
  • IDEA Intellij
  • Kotlin 1.2.31
 
快速构建
 
首先构建一个maven项目,我们命名为KotlinDemo。(使用idea构建Kotlin项目需要按照Kotlin的插件。)
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中添加插件
 
<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>
2.创建application.yml
server:
  context-path: /kotlin
  port: 8090
3.创建Application.kt文件
 
       创建类的方法同java相似,也是引入各种包,但是需要注意Kotlin的main方法是class外面和java的main方法区别

        添加spring的注解@RestController和@SpringBootApplication,我们在class里面创建一个简单的restfull格式的请求方法hello(),方法返一个String的字符串。

       添加@RequestMapping时候需要注意设置method的时候,这里method接收的是一个数组,不是RequestMethod.GE,所以我们需要通过kotlin提供的arrayOf()方法转换一个下,这是一个创建数据的方法,具体的可以参考一下kotlin的文档。

       注意一下 SpringApplication.run中参数这个和java的区别有点大
 
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);

}

3.项目基本搭建完成,试试启动项目访问http://localhost:8090/demo/hello,页面会返回hello kotlin。


转载http://www.kotliner.net/?/article/7

技术资料共享交流群 QQ:566457387

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值