从去年开始就开始学习kotlin了,可是一直没有时间总结本身学习的东西,如今终于有点时间了,所将整理一套SpringBoot kotlin 的开发教程,但愿可以帮组更多想从Java转Kotlin的朋友。
Kotlin是一门静态语言,支持多种平台,包括移动端、服务端以及浏览器端,此外,Kotlin仍是一门融合了面向对象与函数式编程的语言,支持泛型、安全的空判断,而且Kotlin与Java能够作到彻底的交互。java
<?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>
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) }
@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)
@GetMapping("/mono") fun helloMono(): Mono<String> { return Mono.just("Hello Mono") }
若是你们以为文章有用麻烦点一下赞,有问题的地方欢迎你们指出来。react