我尝试从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生成器Gradle插件文档进行操作。
我的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存在语法错误,生成器会正确收集文件(如果文件中存在任何语法错误,则构建失败)。
构建日志显示“成功将代码生成到属性…”看起来有些可疑。这是否意味着该属性包含了生成的结果?
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插件的例子,上面的代码适用于Gradle v4,但不适用于v5。我仍在调查中。
java/main
只是为了测试,以确保我没有遗漏目录的问题。 最终它开始工作了,但我注意到它生成了整个项目,而不仅仅是 Spring 代码(这不是我期望的)。现在我需要深入研究文档,希望有一种开关可以禁用此功能。 谢谢! - zolvmodelFilesConstrainedTo
就解决了问题,谢谢 - Shmarkus