SpringBoot 2.X Kotlin 系列之Hello World

SpringBoot 2.X Kotlin 系列之Hello World

从去年开始就开始学习kotlin了,但是一直没有时间总结自己学习的东西,现在终于有点时间了,所将整理一套SpringBoot kotlin 的开发教程,希望能够帮组更多想从Java转Kotlin的朋友。

一、Kotlin简介

Kotlin是一门静态语言,支持多种平台,包括移动端、服务端以及浏览器端,此外,Kotlin还是一门融合了面向对象与函数式编程的语言,支持泛型、安全的空判断,并且Kotlin与Java可以做到完全的交互。

二、教程环境

  • JAVA "11.0.2"
  • maven 3.5.3
  • mongodb 4.0
  • springBoot 2.1.3
  • kotlin 1.3.21

三、创建项目

  • 二是在IDEA上创建如图所示

  • 勾选Reactive Web 然后点NEXT,然后一直默认最后点击完成即可。
  • 然后我们看到以下的项目结构

  • POM文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>io.intodream</groupId>
	<artifactId>kotlin01</artifactId>
	<version>1.0.0</version>
	<name>kotlin01</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<kotlin.version>1.3.21</kotlin.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webflux</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.module</groupId>
			<artifactId>jackson-module-kotlin</artifactId>
		</dependency>
		<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>
		</dependency>
		<dependency>
			<groupId>io.projectreactor</groupId>
			<artifactId>reactor-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
		<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.jetbrains.kotlin</groupId>
				<artifactId>kotlin-maven-plugin</artifactId>
				<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>


复制代码

四、HelloWord

  • 项目创建完后,我们看到了启动类和我和熟悉的Java SpringBoot几乎是一样的,正如官方所说的完全兼容Java,所以我们就不用担心了。

package io.intodream.kotlin01

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class Kotlin01Application

fun main(args: Array<String>) {
	runApplication<Kotlin01Application>(*args)
}


复制代码
  • 下面我们就来写一个Hello World
@RequestMapping("/rest")
@RestController
class HelloController {

    @GetMapping("/hello")
    fun hello (): String {
        return "Hello World"
    }
}
复制代码
  • 写完后我们开始运行项目,看到控制台输入一下信息则说明运行完毕
2019-03-24 17:03:53.848  INFO 4342 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8080
2019-03-24 17:03:53.863  INFO 4342 --- [           main] i.i.kotlin01.Kotlin01ApplicationKt       : Started Kotlin01ApplicationKt in 3.434 seconds (JVM running for 8.546)
复制代码
  • 打开浏览器输入http://localhost:8080/rest/hello,我们会看到下面的信息,第一个SpringBoot Kotlin项目我们就写好了。

  • 我们在创建项目的时候选择的是Reactive Web,而不是传入Web,也就是说我们可以编写响应式的Web程序,下面就编写一个简单的响应式接口。
@GetMapping("/mono")
    fun helloMono(): Mono<String> {
        return Mono.just("Hello Mono")
    }
复制代码
  • 我们这里看到和普通的接口没有异同,除了返回类型是用Mono包装之外。与之对应的还有Flux,这个后面我们会讲到。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值