1. Javaプロジェクト作成
- ブラウザを開き、(https://start.spring.io/)を入力し、開く
- 下記を入力し、Dependenciesで下記を選択し、『GENERATE』ボタンを押す
-
Projects: Gradle Projects
-
Lnguage: Java
-
Spring Boot: 3.1.0
-
Group: com.example
-
Artifact: todo_api
-
Name: todo_api
-
Description: todo_api
-
Package name: com.example.todo_api
-
Dependencies: Jar
-
Java: 11
Dependencies
- Spring Web
2. スキーマ駆動開発の準備をする
※ スキーマ駆動開発とは、スキーマを起点にしてさまざまなものを開発していくという開発手法
※ YAMLとは、構造化データやオブジェクトを文字列にシリアライズするためのデータ形式
3. スキーマファイルをする
①src/main/resouces/api-schema.yamlを作成する
スキーマの記法
公式ドキュメント : OpenApi
②OPENAPIGENERATORGRADLEプラグインを導入する
plugins {
//追加
id 'org.openapi.generator' version '3.1.0'
}
③スキーマからAPIドキュメントを生成する
plugins {
id 'java'
id 'org.springframework.boot' version '3.1.0'
id 'io.spring.dependency-management' version '1.1.0'
id 'org.openapi.generator' version '3.1.0'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
task buildApiDoc(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
generatorName="html2"
inputSpec="$rootDir/src/main/resources/api-schema.yaml".toString()
outputDir="$buildDir/apidoc".toString()
}
③スキーマからAPIドキュメントを生成する
openapi: '3.1.0'
info:
title: TODO API Document
version: '0.0.1'
description: TODO APIのドキュメントです
paths:
/health:
get:
responses:
'200':
description: OK
plugins {
id 'java'
id 'org.springframework.boot' version '3.1.0'
id 'io.spring.dependency-management' version '1.1.0'
id 'org.openapi.generator' version '3.1.0'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
task buildApiDoc(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
generatorName="html2"
inputSpec="$rootDir/src/main/resources/api-schema.yaml".toString()
outputDir="$buildDir/apidoc".toString()
}
④スキーマから Spring のコードを生成する
plugins {
id 'java'
id 'org.springframework.boot' version '3.1.0'
id 'io.spring.dependency-management' version '1.1.0'
id 'org.openapi.generator' version '3.1.0'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
task buildApiDoc(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
generatorName="html2"
inputSpec="$rootDir/src/main/resources/api-schema.yaml".toString()
outputDir="$buildDir/apidoc".toString()
}
task buildSpringServer(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
generatorName="spring"
inputSpec="$rootDir/src/main/resources/api-schema.yaml".toString()
outputDir="$buildDir/spring".toString()
apiPackage="com.example.todoapi.controller"
modelPackage="com.example.todoapi.model"
configOptions=[
interfaceOnly: "true"
]
}
⑤./gradlew runをする
⑤src/main/java/com/example/todo_apiにcontrollerフォルダと
ApiUtil.javaとHealthApi.javaを作成する
⑥Gradleタスクの依存関係を設定する
//省略
comileJava.dependsOn tasks.buildSpringServer
生成したコードをインポートできるようにする
①⑤src/main/java/com/example/todo_apiにcontrollerフォルダと
HealthApi.javaを作成する