Open4

Kotlin開発環境構築

ふじしろふじしろ

InteliJをインストール

https://www.jetbrains.com/ja-jp/idea/download/#section=mac

  • MacOs
  • Community Edition(無償版)
  • Apple Silicon

Spring Initializrで雛形生成

https://start.spring.io/
書籍と現在(2023/02/17)の設定画面では一部違う部分がある。以下のように設定してみた。

  • 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に表示できる
https://spring.pleiades.io/spring-framework/docs/current/javadoc-api/org/springframework/stereotype/Controller.html

@RestController
RestAPI用コントローラであることを宣言するアノテーション
戻り値のオブジェクトをJSON形式にシリアライズしてレスポンスとして返す。
https://spring.pleiades.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html

@RequestParam:クエリパラメータ指定
@PathVariable:パスパラメータ指定
(※@PathParamアノテーションと間違えた。注意。)