從去年開始就開始學習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