找到 Swagger 并为“宠物”示例生成一个 WebAPI 项目后,我认为下一个逻辑步骤是生成后端数据库。然而,我遇到的导师都没有推荐这种方法。
我发现方法存根中没有实现异常,因此我认为它对数据库来说是不可知的。我的问题:
a) 是否有一种“推荐”的方法来生成 SQL 表,就像使用脚手架从模型类生成 API 一样?
b) 当 API 细节发生变化时,可接受的工作流程是什么?从 Swagger 编辑器完全重新生成服务器项目,然后手动将对存储库的调用复制到每个存根方法中?然后更改模型后使用迁移?手动复制会非常耗时。
我并不在 .net core core 或 aspnet WebAPI 之间烦恼,无论什么工作我都很乐意使用。
谢谢
我正在尝试做类似以下的事情:
在我的架构 JSON 模型部分:
"MyObject": {
"type": "object",
"description": "my description",
"properties": {
"type": "string",
"description": "my property description",
"customAnnotation": "true"
}
}
Run Code Online (Sandbox Code Playgroud)
因此,我一开始就尝试扩展 JSON Schema - 可能是我的第一个问题。但是,我不知道如何合法地做到这一点(如果可能的话)。
“customAnnotation”胡子模板的用例片段(-l spring):
{{#vars}}
{{^customAnnotation}}@CustomAnnotation {{/customAnnotation}}public {{{datatypeWithEnum}}} {{getter}}() {
return {{name}};
}
{{/vars}}
Run Code Online (Sandbox Code Playgroud)
我真的可以做这样的事情吗?线索很有帮助(是的,我是这个领域的新手)!
注意:我还想使用找到的“customAnnotation”> 0 的计数来注释类。就像是:
{{^containsCustomAnnotations}}@ContainsCustomAnnotations {{/hasCustomAnnotation}}public void MyClass {
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我有一个用 OpenAPI 2.0 记录的休息服务。它通过自签名 ssl 证书进行保护,该证书无法替换,因为该服务很可能会localhost在生产中运行。
我使用 swagger-codegen-maven-plugin 根据 OpenAPi 规范生成客户端。客户端似乎可以工作,但拒绝自签名证书。
底层的http库是okhttp,它能够接受自签名证书,但由于这种情况,我需要能够随时重新生成我的客户端,我无法在生成的客户端中合并必要的更改。
我怎样才能okhttp接受我的证书?
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.playground</groupId>
<artifactId>api-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<gson-fire.version>1.8.0</gson-fire.version>
<swagger-core.version>1.5.15</swagger-core.version>
<okhttp.version>2.7.5</okhttp.version>
<gson.version>2.8.1</gson.version>
<threetenbp.version>1.3.5</threetenbp.version>
<junit-version>4.12</junit-version>
<swagger-codegen-maven-plugin.version>2.3.1</swagger-codegen-maven-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>com.playground</groupId>
<artifactId>playground-api</artifactId>
<version>0.1.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- Tag: Swagger SDK Dependencies -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-core.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>logging-interceptor</artifactId>
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency> …Run Code Online (Sandbox Code Playgroud) 我在多目录(包)结构中使用了 swagger 模型(使用 Java JAX-RS)。
我的目标是使用 Swagger 代码生成器来创建客户端代码,同时将模型类放在不同的包中,使用目录(包)的名称或任何其他 swagger 特定的元数据(例如注释)。
我想要我的包裹
由于 Swagger codegen 输入是 JSON 或 YAML,它是从我的 Java 类生成的,因此包信息必须位于 openAPI 定义中,或者传递给 codegen。我找到了指定模型包的选项,该模型包对于所有类都相同,但我希望它们位于不同的包中。
该主题已发布到 Swagger github issues,但已关闭并建议修改 codegen 源: https: //github.com/swagger-api/swagger-codegen/issues/4634
我正在使用 swagger-codegen 生成 JAVA 类。
我想用 ResponseEntity 生成一个类ResponseEntity<InputStreamResource>。
例如:
ResponseEntity<InputStreamResource> pmml(@RequestParam(value = "modelUuid") String modelUuid);
Run Code Online (Sandbox Code Playgroud)
我使用下面的 yaml 生成 JAVA 类。
paths:
/report:
get:
summary: Returns the report in the PDF format
responses:
'200':
description: A PDF file
content:
application/octet-stream:
schema:
type: string
format: binary
Run Code Online (Sandbox Code Playgroud)
但它只生成 java 类,如下所示:
paths:
/report:
get:
summary: Returns the report in the PDF format
responses:
'200':
description: A PDF file
content:
application/octet-stream:
schema:
type: string
format: binary
Run Code Online (Sandbox Code Playgroud) 我第一次尝试使用 swagger-codegen 为我的 REST 服务自动生成 java 客户端。我下载了 cli 并运行了这个命令:
java -jar ~/Downloads/swagger-codegen-cli-2.2.1.jar generate \
-i http://my.services.url/swagger.json \
-l java
Run Code Online (Sandbox Code Playgroud)
生成的代码使用 okhttp 客户端实现。我真的很想利用 CompletableFutures,但这似乎远远超出了 swagger 所能提供的范围。有谁知道从 swagger codegen 生成 asynchttp 客户端的方法吗?这是一个已解决的问题还是需要通过扩展源代码DefaultCodegen?
我正在使用OpenApi v3.3.4(以前称为Swagger CodeGen)maven 插件通过文件生成我的其余控制器api.yml,其中我描述了我想要公开的所有操作。
在我的用例中,我想公开一个方法POST: handleNotification(@RequestBody SignatureNotification notification),其请求正文的类型是通过/targer文件夹中的另一个 maven-plugin 生成的。
实际上我SignatureNotification在Components.yml 文件的一部分中定义:
...
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/SignatureNotification'
...
Run Code Online (Sandbox Code Playgroud)
它由 OpenApi 插件生成,然后我将其映射到SignatureNotification已经存在且具有相同属性的插件。
我对这个解决方案不太满意,所以我想知道是否有办法告诉 OpenApi Generator 使用外部对象作为引用?
我有以下结构
Notification
|
------------------------
| |
SmsNotification EmailNotification
Run Code Online (Sandbox Code Playgroud)
包含一个包含 或的Notification枚举。现在我有一个类,其中包含一个.notificationTypeSMSEMAILInboxNotification
这是在 swagger yml 中指定的(删除了一些不相关的代码)
definitions:
Notification:
type: "object"
discriminator: "notificationType"
properties:
notificationType:
type: "string"
description: "Type of notification"
enum:
- "EMAIL"
- "SMS"
SmsNotification:
allOf:
- $ref: "#/definitions/Notification"
- type: "object"
EmailNotification
allOf:
- $ref: "#/definitions/Notification"
- type: "object"
Inbox:
type: "object"
properties:
notification:
description: "Latest received notification"
$ref: "#/definitions/Notification"
Run Code Online (Sandbox Code Playgroud)
swagger-codegen v2我使用以下配置生成代码(也尝试过 v3 和 openapi-generator):
<build>
<plugins>
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.3.1</version>
<executions> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 json 正文的示例来记录 api 错误响应。我找不到示例或合适的注释。使用 swagger 编辑器,我至少可以获得一些看起来像我想要实现的结果的东西。
responses:
'200' :
description: Request completed with no errors
examples:
application/json: {"result" : { "id": "blue" }}
Run Code Online (Sandbox Code Playgroud)
库是 swagger-core 1.6.0
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-core</artifactId>
<scope>compile</scope>
<version>1.6.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
端点是使用 jax-rs 创建的。
我对端点做了这个
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK",
examples = @Example(value = @ExampleProperty(mediaType = "application/json", value = "{\"result\" : { \"id\": \"blue\" }}"))
)
})
public Response getResult(){}
Run Code Online (Sandbox Code Playgroud)
生成的 swagger.json 没有所需的
examples:
application/json: {"result" : { "id": "blue" }}
Run Code Online (Sandbox Code Playgroud)
我也尝试传递response = ApiResponse.class、Examples.class和Example.class,但它没有改变。 …
我在https://editor.swagger.io中加载了一个 OpenAPI YAML 文件,并从那里生成了 jaxrs-resteasy 的服务器代码。
问题是此代码必须集成到 Java 7 项目中,并且 Swagger 编辑器会生成 Java 8 的代码。
有没有办法生成 Java 7 的 jaxrs-resteasy 服务器代码?
swagger-codegen ×10
swagger ×7
java ×3
openapi ×2
c# ×1
java-7 ×1
jax-rs ×1
jsonschema ×1
mustache ×1
self-signed ×1
ssl ×1
swagger-2.0 ×1