{"id":271,"date":"2020-04-24T21:38:36","date_gmt":"2020-04-24T12:38:36","guid":{"rendered":"http:\/\/localhost:8000\/?p=271"},"modified":"2021-01-17T21:32:20","modified_gmt":"2021-01-17T12:32:20","slug":"kotlin-ktor-clean-architecture","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2020\/04\/kotlin-ktor-clean-architecture.html","title":{"rendered":"Kotlin+Ktor\u3067\u6280\u8853\u8981\u7d20\u3092\u8a66\u3057\u306a\u304c\u3089\u30af\u30ea\u30fc\u30f3\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u3067API\u30b5\u30fc\u30d0\u69cb\u7bc9"},"content":{"rendered":"
\u30b5\u30fc\u30d0\u30b5\u30a4\u30c9Kotlin\u3068\u3044\u3048\u3070Spring Boot<\/a>\u3092\u63a1\u7528\u3059\u308b\u3053\u3068\u304c\u591a\u3044\u3068\u601d\u3044\u307e\u3059\u3002 JetBrains\u793e\u88fd\u306eWeb\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3042\u308bKtor<\/a>\u304c\u30012018\u5e7411\u6708\u306bv1.0.0\u304c\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u30012020\u5e744\u6708\u73fe\u5728v1.3.2\u307e\u3067\u9806\u8abf\u306b\u30a2\u30c3\u30d7\u30c7\u30fc\u30c8\u3055\u308c\u7d9a\u3051\u3066\u304a\u308a\u3001\u305d\u308d\u305d\u308d\u672c\u683c\u7684\u306b\u5229\u7528\u3057\u3066\u3082\u826f\u3055\u305d\u3046\u306a\u6c17\u914d\u3092\u611f\u3058\u3066\u3044\u307e\u3059\u3002<\/p>\n \u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001Kotlin+Ktor\u3067REST API\u306e\u4f5c\u6210\u306b\u5fc5\u8981\u306a\u6280\u8853\u8981\u7d20\u3092Step By Step\u3067\u691c\u8a3c\u3057\u3066\u307f\u306a\u304c\u3089\u3001\u30af\u30ea\u30fc\u30f3\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u306aAPI\u30b5\u30fc\u30d0\u3092\u69cb\u7bc9\u3057\u3066\u307f\u305f\u3044<\/u>\u3068\u601d\u3044\u307e\u3059\u3002 Ktor Plugin<\/a>\u3092\u4f7f\u3046\u65b9\u6cd5\u3060\u3068\u826f\u304f\u8a2d\u5b9a\u5185\u5bb9\u3092\u7406\u89e3\u305b\u305a\u306b\u4f7f\u3063\u3066\u3057\u307e\u3046\u3053\u3068\u306b\u306a\u308a\u305d\u3046\u3067\u3059\u3057\u3001Docker\u3092\u4f7f\u3046\u65b9\u6cd5<\/a>\u3060\u3068HotReload\u304c\u3067\u304d\u306a\u3055\u305d\u3046\u306a\u306e\u3067\u3001\u4eca\u56de\u306fGradle\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u308b\u65b9\u6cd5<\/a>\u3092\u8a66\u3057\u3066\u884c\u3053\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n \u307e\u305a\u3001Gradle\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067kotlin\u3092\u9078\u629e\u3057\u307e\u3059\u3002 Gradle\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u597d\u307f\u3067\u8a2d\u5b9a\u3057\u307e\u3059\u3002 \u4f5c\u6210\u3092\u9032\u3081Finish\u3059\u308b\u3068\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304c\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002<\/p>\n \u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u751f\u6210\u3055\u308c\u305f
\n\u500b\u4eba\u7684\u306b\u306fSpring Boot\u306f\u3001Auto Configuration\u306e\u30d6\u30e9\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u611f\u304c\u8f9b\u304f\u3066\u3001\u3082\u3063\u3068\u30b7\u30f3\u30d7\u30eb\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306b\u4e57\u308a\u63db\u3048\u305f\u3044\u3068\u3044\u3046\u601d\u3044\u304c\u5e38\u3005\u3042\u308a\u307e\u3057\u305f\u3002<\/p>\n
\n<\/p>\n\u6700\u521d\u306e\u753b\u9762\u3092\u8868\u793a<\/h2>\n
IntelliJ\u3067Gradle\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u4f5c\u6210<\/h3>\n
\n<\/p>\n
\n<\/p>\n\n
Use auto-import<\/code>\u306f\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u81ea\u52d5\u540c\u671f\u3092\u3057\u3066\u304f\u308c\u308b\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059<\/li>\n
Create separate module per source set<\/code>\u306fsrc\u3068test\u3092\u5225module\u3068\u3057\u3066\u51fa\u529b\u3057\u3066\u3001\u4f9d\u5b58\u30e9\u30a4\u30d6\u30e9\u30ea\u3082\u5225\u7ba1\u7406\u306b\u3059\u308b\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059<\/li>\n
$ tree\n.\n\u251c\u2500\u2500 build.gradle\n\u251c\u2500\u2500 gradle\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 wrapper\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 gradle-wrapper.jar\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 gradle-wrapper.properties\n\u251c\u2500\u2500 gradle.properties\n\u251c\u2500\u2500 gradlew\n\u251c\u2500\u2500 gradlew.bat\n\u2514\u2500\u2500 settings.gradle\n\n2 directories, 7 files<\/code><\/pre>\n
build.gradle\u306bKtor\u306eGradle\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u7528\u306e\u5185\u5bb9\u3067\u7f6e\u304d\u63db\u3048<\/h3>\n
build.gradle<\/code>\u306e\u5185\u5bb9\u3092\u3053\u3061\u3089<\/a>\u306e\u5185\u5bb9\u3067\u66f8\u304d\u63db\u3048\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308aktor-server-netty\u306a\u3069\u306e\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u307e\u3059\u3002<\/p>\n
group 'rinoguchi'\nversion '1.0-SNAPSHOT'\n\nbuildscript {\n ext.kotlin_version = '1.3.70'\n ext.ktor_version = '1.3.1' \/\/ mavenCentral\u306b\u5b58\u5728\u3059\u308bversion\u3092\u6307\u5b9a\u3059\u308b\n\n repositories {\n mavenCentral()\n }\n dependencies {\n classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"\n }\n}\n\napply plugin: 'java'\napply plugin: 'kotlin'\napply plugin: 'application'\n\nmainClassName = 'MainKt'\n\nsourceCompatibility = 1.8\ncompileKotlin { kotlinOptions.jvmTarget = "1.8" }\ncompileTestKotlin { kotlinOptions.jvmTarget = "1.8" }\n\nrepositories {\n mavenCentral()\n}\n\ndependencies {\n implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"\n implementation "io.ktor:ktor-server-netty:$ktor_version"\n implementation "ch.qos.logback:logback-classic:1.2.3"\n testImplementation group: 'junit', name: 'junit', version: '4.12'\n}<\/code><\/pre>\n
main\u95a2\u6570\u3092\u6301\u3064Application.kt\u3092\u4f5c\u6210<\/h3>\n
package sample\n\nimport io.ktor.application.*\nimport io.ktor.http.*\nimport io.ktor.response.*\nimport io.ktor.routing.*\nimport io.ktor.server.engine.*\nimport io.ktor.server.netty.*\n\nfun main(args: Array<String>) {\n embeddedServer(Netty, 8080) {\n routing {\n get("\/") {\n call.respondText("First Sample", ContentType.Text.Html)\n }\n }\n }.start(wait = true)\n}<\/code><\/pre>\n
\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3057\u3066\u30d6\u30e9\u30a6\u30b6\u3067\u78ba\u8a8d<\/h3>\n
Application.kt<\/code>\u30d5\u30a1\u30a4\u30eb\u3092\u53f3\u30af\u30ea\u30c3\u30af\u3057\u3066
Run Application.kt<\/code>\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n