从 OpenAPI 生成 Java Spring API 3
Generate Java Spring API from OpenAPI 3
我尝试从 OpenAPI v3 YAML 文件生成 Spring REST 接口。
构建说:
Successfully generated code to property(class java.lang.String, property(class java.lang.String, fixed(class java.lang.String, /home/zolv/workspaces/main/generation-test/src/main/java)))
Deprecated Gradle features were used in this build, making it
incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.5/userguide/command_line_interface.html#sec:command_line_warnings
但是在输出目录中没有生成代码。
我关注了 OpenAPI generator gradle plugin doc 医生。
我的build.gradle:
plugins {
id 'java-library'
id "org.openapi.generator" version "4.1.1"
}
repositories {
jcenter()
}
dependencies {
implementation "org.openapitools:openapi-generator-gradle-plugin:3.3.4"
}
openApiGenerate {
generatorName = "spring"
inputSpec = "$rootDir/docs/openapi/api.yml".toString()
outputDir = "$rootDir/src/main/java".toString()
apiPackage = "org.openapi.example.api"
invokerPackage = "org.openapi.example.invoker"
modelPackage = "org.openapi.example.model"
modelFilesConstrainedTo = [
"Error"
]
configOptions = [
dateLibrary: "java8"
]
}
我的api.yml:
openapi: 3.0.2
info:
title: API Documentation
version: 1.0.0
servers:
- url: http://localhost:8080/
tags:
- name: Users
paths:
/users:
post:
tags:
- Users
summary: Create user
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/CreateUserRequest'
responses:
201:
description: New user created
content:
application/json:
schema:
$ref: '#/components/schemas/UserResponse'
components:
schemas:
CreateUserRequest:
title: CreateUserResponse
required:
- username
- password
type: object
properties:
username:
type: string
description: Users's username
example: example@test.com
password:
type: string
description: User's password
example: $tR0nG_pA55vv0Rd
UserResponse:
title: UserResponse
required:
- id
- username
type: object
properties:
id:
type: string
description: Users's identifier
example: "1"
username:
type: string
description: Users's username
example: example@test.com
api.yml 由生成器正确收集(如果文件中有任何语法错误,构建将失败)。
构建日志显示的内容看起来很可疑:Successfully generated code to property(...
。不是说 属性 包含生成的结果吗?
Gradle版本:
------------------------------------------------------------
Gradle 5.5
------------------------------------------------------------
Build time: 2019-06-28 17:36:05 UTC
Revision: 83820928f3ada1a3a1dbd9a6c0d47eb3f199378f
Kotlin: 1.3.31
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.14 compiled on March 12 2019
JVM: 11.0.4 (Ubuntu 11.0.4+11-post-Ubuntu-1ubuntu218.04.3)
OS: Linux 4.15.0-1050-oem amd64
编辑:
我已经检查了来自 gradle plugin 的示例,上面的代码适用于 Gradle v4,但不适用于 5。我仍在调查中。
问题似乎出在 modelFilesConstrainedTo
— 它将 类 限制为 Error
。将其注释掉,它将起作用(将生成类)。
但是,还有一个问题:outputDir
。根据您的设置,它将生成如下内容:
错了。此外,由于它是 生成的 源,因此不应转到 src/main
。更好的替代方法是在 build
目录中生成它,然后将其添加到编译类路径中。看看我为你创建的 the demo 项目,这里是 build.gradle.kts
(它几乎像 Groovy):
plugins {
id("java-library")
id("org.openapi.generator").version("4.0.1")
id("org.springframework.boot").version("2.1.8.RELEASE")
}
repositories {
jcenter()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web:2.1.8.RELEASE")
api("io.springfox:springfox-swagger2:2.8.0")
api("io.springfox:springfox-swagger-ui:2.8.0")
api("org.openapitools:jackson-databind-nullable:0.1.0")
}
val spec = "$rootDir/docs/openapi/api.yml"
val generatedSourcesDir = "$buildDir/generated/openapi"
openApiGenerate {
generatorName.set("spring")
inputSpec.set(spec)
outputDir.set(generatedSourcesDir)
apiPackage.set("org.openapi.example.api")
invokerPackage.set("org.openapi.example.invoker")
modelPackage.set("org.openapi.example.model")
configOptions.set(mapOf(
"dateLibrary" to "java8"
))
}
sourceSets {
getByName("main") {
java {
srcDir("$generatedSourcesDir/src/main/java")
}
}
}
tasks {
val openApiGenerate by getting
val compileJava by getting {
dependsOn(openApiGenerate)
}
}
请注意,您还需要一堆在生成源中使用的依赖项(注释和 Spring 类)。
尝试生成内部源文件夹如下
openApiGenerate {
generatorName = "spring"
inputSpec = "$rootDir/src/main/resources/petstore-simple.json".toString()
outputDir = "$rootDir".toString()
apiPackage = "com.example.api"
invokerPackage = "com.example.api"
modelPackage = "com.example.model"
configOptions = [
dateLibrary: "java8"
]
globalProperties = [
modelDocs: "false"
]
}
我尝试从 OpenAPI v3 YAML 文件生成 Spring REST 接口。 构建说:
Successfully generated code to property(class java.lang.String, property(class java.lang.String, fixed(class java.lang.String, /home/zolv/workspaces/main/generation-test/src/main/java)))
Deprecated Gradle features were used in this build, making it
incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.5/userguide/command_line_interface.html#sec:command_line_warnings
但是在输出目录中没有生成代码。
我关注了 OpenAPI generator gradle plugin doc 医生。
我的build.gradle:
plugins {
id 'java-library'
id "org.openapi.generator" version "4.1.1"
}
repositories {
jcenter()
}
dependencies {
implementation "org.openapitools:openapi-generator-gradle-plugin:3.3.4"
}
openApiGenerate {
generatorName = "spring"
inputSpec = "$rootDir/docs/openapi/api.yml".toString()
outputDir = "$rootDir/src/main/java".toString()
apiPackage = "org.openapi.example.api"
invokerPackage = "org.openapi.example.invoker"
modelPackage = "org.openapi.example.model"
modelFilesConstrainedTo = [
"Error"
]
configOptions = [
dateLibrary: "java8"
]
}
我的api.yml:
openapi: 3.0.2
info:
title: API Documentation
version: 1.0.0
servers:
- url: http://localhost:8080/
tags:
- name: Users
paths:
/users:
post:
tags:
- Users
summary: Create user
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/CreateUserRequest'
responses:
201:
description: New user created
content:
application/json:
schema:
$ref: '#/components/schemas/UserResponse'
components:
schemas:
CreateUserRequest:
title: CreateUserResponse
required:
- username
- password
type: object
properties:
username:
type: string
description: Users's username
example: example@test.com
password:
type: string
description: User's password
example: $tR0nG_pA55vv0Rd
UserResponse:
title: UserResponse
required:
- id
- username
type: object
properties:
id:
type: string
description: Users's identifier
example: "1"
username:
type: string
description: Users's username
example: example@test.com
api.yml 由生成器正确收集(如果文件中有任何语法错误,构建将失败)。
构建日志显示的内容看起来很可疑:Successfully generated code to property(...
。不是说 属性 包含生成的结果吗?
Gradle版本:
------------------------------------------------------------
Gradle 5.5
------------------------------------------------------------
Build time: 2019-06-28 17:36:05 UTC
Revision: 83820928f3ada1a3a1dbd9a6c0d47eb3f199378f
Kotlin: 1.3.31
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.14 compiled on March 12 2019
JVM: 11.0.4 (Ubuntu 11.0.4+11-post-Ubuntu-1ubuntu218.04.3)
OS: Linux 4.15.0-1050-oem amd64
编辑:
我已经检查了来自 gradle plugin 的示例,上面的代码适用于 Gradle v4,但不适用于 5。我仍在调查中。
问题似乎出在 modelFilesConstrainedTo
— 它将 类 限制为 Error
。将其注释掉,它将起作用(将生成类)。
但是,还有一个问题:outputDir
。根据您的设置,它将生成如下内容:
错了。此外,由于它是 生成的 源,因此不应转到 src/main
。更好的替代方法是在 build
目录中生成它,然后将其添加到编译类路径中。看看我为你创建的 the demo 项目,这里是 build.gradle.kts
(它几乎像 Groovy):
plugins {
id("java-library")
id("org.openapi.generator").version("4.0.1")
id("org.springframework.boot").version("2.1.8.RELEASE")
}
repositories {
jcenter()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web:2.1.8.RELEASE")
api("io.springfox:springfox-swagger2:2.8.0")
api("io.springfox:springfox-swagger-ui:2.8.0")
api("org.openapitools:jackson-databind-nullable:0.1.0")
}
val spec = "$rootDir/docs/openapi/api.yml"
val generatedSourcesDir = "$buildDir/generated/openapi"
openApiGenerate {
generatorName.set("spring")
inputSpec.set(spec)
outputDir.set(generatedSourcesDir)
apiPackage.set("org.openapi.example.api")
invokerPackage.set("org.openapi.example.invoker")
modelPackage.set("org.openapi.example.model")
configOptions.set(mapOf(
"dateLibrary" to "java8"
))
}
sourceSets {
getByName("main") {
java {
srcDir("$generatedSourcesDir/src/main/java")
}
}
}
tasks {
val openApiGenerate by getting
val compileJava by getting {
dependsOn(openApiGenerate)
}
}
请注意,您还需要一堆在生成源中使用的依赖项(注释和 Spring 类)。
尝试生成内部源文件夹如下
openApiGenerate {
generatorName = "spring"
inputSpec = "$rootDir/src/main/resources/petstore-simple.json".toString()
outputDir = "$rootDir".toString()
apiPackage = "com.example.api"
invokerPackage = "com.example.api"
modelPackage = "com.example.model"
configOptions = [
dateLibrary: "java8"
]
globalProperties = [
modelDocs: "false"
]
}