Open4
Kotlin開発環境構築
目的:Kotlinの基本を学び直し、自分の知識として定着させる
開発環境構築
Controller
RestController
参考書
InteliJをインストール
- MacOs
- Community Edition(無償版)
- Apple Silicon
Spring Initializrで雛形生成
- project: Gradle-Kotlin
- langage: Kotlin
- SpringBoot:
3.0.3※ 2.7.9 - Packaging: Jar
- Java: 11
- Dependencies:
- Spring Web
- Thymeleaf
※SpringBootを3.0.3に設定し、IntelliJで実行しようとするとエラーになった。
-> Javaのバージョンの互換性の問題らしく、SpringBoot3はJava17が必要とのこと。
-> SpringBoot3.x.xでJava17以上を入れるか、SpringBoot2.x.xで生成する必要がありそうに感じた。
-> 今回は書籍にできるだけ近い状態でやりたいため、SpringBootを2.7.9に変更した(エラーも消えた)
A problem occurred configuring root project 'demo'.
> Could not resolve all files for configuration ':classpath'.
> Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.0.3.
Required by:
project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.0.3
> No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.0.3 was found. The consumer was configured to find a runtime of a library compatible with Java 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.6' but:
- Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.3 declares a library, packaged as a jar, and its dependencies declared externally:
- Incompatible because this component declares an API of a component compatible with Java 17 and the consumer needed a runtime of a component compatible with Java 11
- Other compatible attribute:
- Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
- Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.3 declares a runtime of a component, and its dependencies declared externally:
- Incompatible because this component declares documentation and the consumer needed a library
- Other compatible attributes:
- Doesn't say anything about its target Java version (required compatibility with Java 11)
- Doesn't say anything about its elements (required them packaged as a jar)
- Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
- Variant 'mavenOptionalApiElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.0.3 declares a library, packaged as a jar, and its dependencies declared externally:
- Incompatible because this component declares an API of a component compatible with Java 17 and the consumer needed a runtime of a component compatible with Java 11
- Other compatible attribute:
- Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
- Variant 'mavenOptionalRuntimeElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.0.3 declares a runtime of a library, packaged as a jar, and its dependencies declared externally:
- Incompatible because this component declares a component compatible with Java 17 and the consumer needed a component compatible with Java 11
- Other compatible attribute:
- Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
- Variant 'runtimeElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.3 declares a runtime of a library, packaged as a jar, and its dependencies declared externally:
- Incompatible because this component declares a component compatible with Java 17 and the consumer needed a component compatible with Java 11
- Other compatible attribute:
- Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
- Variant 'sourcesElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.3 declares a runtime of a component, and its dependencies declared externally:
- Incompatible because this component declares documentation and the consumer needed a library
- Other compatible attributes:
- Doesn't say anything about its target Java version (required compatibility with Java 11)
- Doesn't say anything about its elements (required them packaged as a jar)
- Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
@Controller
Webコントローラであることを宣言するアノテーション
src/resource/template
配下のhtmlファイルのファイル名をString型でreturnすることでそのhtmlページを呼び出す。
引数にmodelを設定し、処理内でmodel.addAtributeでthymeleaf(テンプレートエンジン)に渡したい値を詰めておくことで、任意の値をhtmlに表示できる
@RestController
RestAPI用コントローラであることを宣言するアノテーション
戻り値のオブジェクトをJSON形式にシリアライズしてレスポンスとして返す。
@RequestParam
:クエリパラメータ指定
@PathVariable
:パスパラメータ指定
(※@PathParam
アノテーションと間違えた。注意。)
第2部3章"MyBatisの導入"でハマったところ
./gradlew mbGenerator
でエラーが出る
書籍のGitHubにIssueがあり、これを参考に対応
Issueが参考にしていた記事 Issueの参考にしていた記事で参考にしていた記事