ほのかちゃん
Table of Contents |
PS W:\> kotlinc -version info: kotlinc-jvm 1.3.21 (JRE 1.8.0_201-b09)
わざわざ手元にインストールしないで Kotlin Playground を使わせてもらうのでもいいかも
#highlight(kt){{
fun main(args: Array<String>) {
println("Hello, again my old dear place")
}
}}
PS W:\> kotlinc .\hello.kt -include-runtime -d hello.jar PS W:\> java -jar hello.jar Hello, again my old dear place
#highlight(kt){{
import java.util.Date
import java.text.SimpleDateFormat;
class Person {
val firstName: String
val lastName: String
val birthday: Date?
// "?"がついていなければ null 入れるコードはコンパイルすらできない
constructor(firstName: String, lastName: String, birthday: Date?) {
this.firstName = firstName
this.lastName = lastName
this.birthday = birthday
}
fun fullName(): String {
return "%s %s".format(this.firstName, this.lastName)
}
// 誕生日が null なら何も計算せず(letの中に入らず)に null だけ返す
// 整数型もオブジェクトのように扱えて、null にもできる
fun age(): Int? {
return this.birthday?.let {
val sdf = SimpleDateFormat("yyyyMMdd")
(sdf.format(Date()).toInt() - sdf.format(it).toInt()) / 10000
}
}
}
fun main(args: Array<String>) {
val person1 = Person("Kanata", "Tanaka", SimpleDateFormat("yyyy/MM/dd").parse("2013/12/02"))
println("%s (%s)".format(person1.fullName(), person1.age()?.toString()))
val person2 = Person("Kanata", "Tanaka", null)
// SQLのCOALESCE関数みたいなもの
println("%s (%s)".format(person2.fullName(), person2.age()?.toString() ?: "?"))
}
}}
PS W:\> kotlinc .\age.kt -include-runtime -d age.jar PS W:\> java -jar age.jar Kanata Tanaka (5) Kanata Tanaka (?)
実用性不明。試しに jsoup を手元に置いてから
#highlight(kt){{
import org.jsoup.Jsoup
fun main(args: Array<String>) {
val doc = Jsoup.connect("http://www.capcom.co.jp/arcade/rev/PC/music.html").get()
val elms = doc.select("p.n-mTitle")
for (elm in elms) {
println(elm.text())
}
}
}}
kotlincで生成したJARにGetWebKt.classが含まれるので、これをメインクラスにして起動する(→参考)
PS W:\> kotlinc .\getWeb.kt -classpath .\jsoup-1.11.3.jar -include-runtime -d getWeb.jar PS W:\> java -classpath ".\jsoup-1.11.3.jar;getWeb.jar" GetWebKt Wanna Be Your Special Touch Of Gold :
ベースはこちら → Spring Boot(Kotlin) + GradleでAPIを書いてAzure Web Appにデプロイする。
Spring Initializrに作ってもらう
クラス名定義の後ろに SpringBootServletInitializer() を追加
#highlight(kt){{
package jp.maplia.xxxxx
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer
@SpringBootApplication
class XxxxxApplication: SpringBootServletInitializer()
fun main(args: Array<String>) {
runApplication<XxxxxApplication>(*args)
}
}}
すべて新規
#highlight(kt){{
package jp.maplia.xxxxx
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
class XxxxxController {
@RequestMapping("/")
fun index(): String {
return "To be continued.."
}
}
}}
追加する分だけ記載
warタスクを実行すると、build/libsに生成されたwarが格納される
#highlight(kt){{
apply plugin: 'war'
war {
enabled = true
}
}}
$CATALINA_HOME/webappsに放り込む。環境に手を加えていなければこれだけでおしまい
#highlight(end)