基于maven的kotlin+springboot实例

1. 新建工程

这里以IDEA为例,File-->New-->Project  选择maven工程,输入一个工程名称,例如“springkotlin”

2. 创建kotlin文件夹

在src/main下创建一个kotlin文件夹,选中这个文件夹,右键--> Mark Directory As --> Source Root

在src/test下创建一个kotlin文件夹,选中这个文件夹,右键--> Mark Directory As--> Test Source Root

3. 配置pom.xml

修改pom.xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- 父pom,定义包的依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.16.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <!-- 子工程相关信息 -->
    <groupId>com.example</groupId>
    <artifactId>kotlinspringboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>kolinspringboot</name>
    <description>Demo project for Spring Boot</description>
    <!-- 定义属性 -->
    <properties>
        <java.version>1.8</java.version>
        <kotlin.version>1.3.61</kotlin.version>
    </properties>
    <dependencies>
        <!-- Spring Boot启动包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- Kotlin相关依赖包 -->
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <!-- Kotlin源码路径 -->
        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
        <plugins>
            <!-- Spring Boot Maven打包插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.16.RELEASE</version>
            </plugin>
            <!-- Kotlin Maven插件 -->
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>spring</plugin>
                    </compilerPlugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

 

4. 创建入口类

在src/main/kotlin下面创建一个包 “com.example.springkotlin”,并在此包下在创建一个kotlin类 SpringKotlinApplication

package com.example.springkotlin

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

@SpringBootApplication
open class SpringKotlinApplication

fun main(args: Array<String>) {
    SpringApplication.run(SpringKotlinApplication::class.java, *args)
}

有两个注意事项:

1.class 前要使用open关键字修饰

2.main方法不要放在SpringKotlinApplication类的大括号内

 

5.创建Controller

创建一个简单的IndexController类

package com.example.springkotlin.controller

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

@RestController
class IndexController {

    @GetMapping("/")
    fun index():String{
        return "hello kotlin"
    }
}

6.创建yml配置文件

在resources文件夹下,创建application.yml文件

加入配置

server:
  port: 8088

7. 运行

选中SpringKotlinApplication类,右键,Run

看到日志有以下内容,则项目运行正常

Tomcat started on port(s): 8088 (http) with context path ''
Started SpringKotlinApplicationKt in 16.525 seconds (JVM running for 21.998)

 

在浏览器地址栏中输入 http://localhost:8088/

页面能看到 hello kotlin

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值