使 Swagger 生成工具(Java 类 由它生成)生成服务器期望的自定义 JSON
Make Swagger generation tool (Java classes generated by it) produce a custom JSON that the server is expecting
我正在使用 Swagger 生成的 Java 客户端调用 REST 服务,方法是向其提供 yaml
服务器提供。
服务器对服务的yaml
描述是:
swagger: '2.0'
info:
version: 1.0.0
title: GeoServer Workspace
description: A workspace is a grouping of data stores. Similar to a namespace, it is used to group data that is related in some way.
contact:
name: GeoServer
email: 'geoserver-users@sourceforge.net'
url: 'http://geoserver.org/comm/'
host: localhost:8080
basePath: /geoserver/rest
securityDefinitions:
basic:
type: basic
description: HTTP Basic Authentication.
post:
operationId: postWorkspaces
tags:
- "Workspaces"
summary: add a new workspace to GeoServer
description: Adds a new workspace to the server
security:
- basic: []
parameters:
- name: workspaceBody
description: The layer group body information to upload.
in: body
required: true
schema:
$ref: "#/definitions/Workspace"
- name: default
in: query
description: New workspace will be the used as the default. Allowed values are true or false, The default value is false.
required: false
type: boolean
default: false
consumes:
- application/json
- application/xml
produces:
- text/html
- application/json
- application/xml
responses:
201:
description: Created
schema:
type: string
headers:
Location:
description: URL where the newly created workspace can be found
type: string
401:
description: Unable to add workspace as it already exists
definitions:
Workspace:
title: Workspace
xml:
name: workspace
type: object
properties:
name:
type: string
description: name of the workspace
Swagger 生成器从中创建简单的 类,易于使用:
Workspace workspace = new Workspace();
workspace.setName("test");
WorkspacesApi workspacesApi = new WorkspacesApi(apiClient()); // apiClient() : Authentication
String response = workspacesApi.postWorkspaces(workspace, true);
但是它在调用时产生的内容JSON使服务器失败:
{ name: 'test' }
我发现服务器需要自定义 JSON :
{workspace:{name:'test'}}
并且 Swagger 一代不会响应它的愿望:yaml
包含一个 xml:
属性,它不是 Swagger schema 2.0。该工具不知道如何处理它并忽略它。
definitions:
Workspace:
title: Workspace
xml:
name: workspace
type: object
properties:
name:
type: string
description: name of the workspace
我有办法在 yaml
中输入一些指令来创建服务器正在寻找的自定义 JSON,
将当前生成的 { name: 'test' }
Swagger 生成工具更改为 {workspace:{name:'test'}}
?
或者我是否必须添加一些插件或询问 Swagger 生成工具来覆盖生成的 类(以及如何?)以生成目标自定义 JSON ?
toString()
方法提供的内容是为开发者提供的一些便利功能。它不一定会产生对象的有效 JSON 表示。您应该使用 JSON 序列化程序将 JAVA 对象序列化为 JSON 字符串。例如,这可以在 Gson 库的帮助下完成。
实际上“xml”是 Swagger 2.0 的一部分(参见 https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#xml-modeling),并且在使用 XML 表示时允许对模型进行细粒度控制。
但是您使用的是 JSON,因此由生成的 Swagger 客户端生成的 JSON 负载非常好,并且应该被服务器接受。
这给您带来了两个选择:
a) 通过接受负载修复服务器(100% 符合 API 规范)
b) 编写自定义客户端,将自定义的 JSON 负载发送到服务器。这样生成的Swagger Client不能自定义
@eidottermihi
默认情况下,Swagger 客户端在生成客户端 类.
时接受 JSON
内容
如果我将其 consumes:
声明更改为仅 application/xml
,则在运行时,客户端将抱怨它没有准备好 xml
的处理程序。
问题是你说的那个。编写 yaml
文件的人将其编写为生成 Swagger-UI
文档,但他们是手动完成的,并且 yaml
:
- 不是从服务器 类、
自动创建的
- 或者它不生成服务器实现的基本接口。
这是一个 yaml
写在一边,“我们相信我们的 REST 服务就是这些。”。但是没有保证,没有证据...
因此,无法生成正确的客户端来调用它假装代表的 REST 服务,这毫无用处。
我的问题有答案了:
我能够通过将方法 $ref: "#/definitions/Workspace"
更改为 $ref: "#/definitions/WorkspaceRequest"
来生成对方法 postWorkspaces
的良好 JSON 请求,我已声明:
post:
operationId: postWorkspaces
tags:
- "Workspaces"
summary: add a new workspace to GeoServer
description: Adds a new workspace to the server
security:
- basic: []
parameters:
- name: workspaceBody
description: The layer group body information to upload.
in: body
required: true
schema:
$ref: "#/definitions/WorkspaceRequest"
- name: default
in: query
description: New workspace will be the used as the default. Allowed values are true or false, The default value is false.
required: false
type: boolean
default: false
consumes:
- application/json
- application/xml
produces:
- text/html
- application/json
- application/xml
responses:
201:
description: Created
schema:
type: string
headers:
Location:
description: URL where the newly created workspace can be found
type: string
401:
description: Unable to add workspace as it already exists
definitions:
Workspace:
title: Workspace
xml:
name: workspace
type: object
properties:
name:
type: string
description: name of the workspace
WorkspacesResponse:
title: Workspaces
type: object
properties:
workspaces:
$ref: "#/definitions/WorkspaceResponse"
WorkspaceRequest:
title: Workspace Request
type: object
properties:
workspace:
$ref: "#/definitions/Workspace"
我正在使用 Swagger 生成的 Java 客户端调用 REST 服务,方法是向其提供 yaml
服务器提供。
服务器对服务的yaml
描述是:
swagger: '2.0'
info:
version: 1.0.0
title: GeoServer Workspace
description: A workspace is a grouping of data stores. Similar to a namespace, it is used to group data that is related in some way.
contact:
name: GeoServer
email: 'geoserver-users@sourceforge.net'
url: 'http://geoserver.org/comm/'
host: localhost:8080
basePath: /geoserver/rest
securityDefinitions:
basic:
type: basic
description: HTTP Basic Authentication.
post:
operationId: postWorkspaces
tags:
- "Workspaces"
summary: add a new workspace to GeoServer
description: Adds a new workspace to the server
security:
- basic: []
parameters:
- name: workspaceBody
description: The layer group body information to upload.
in: body
required: true
schema:
$ref: "#/definitions/Workspace"
- name: default
in: query
description: New workspace will be the used as the default. Allowed values are true or false, The default value is false.
required: false
type: boolean
default: false
consumes:
- application/json
- application/xml
produces:
- text/html
- application/json
- application/xml
responses:
201:
description: Created
schema:
type: string
headers:
Location:
description: URL where the newly created workspace can be found
type: string
401:
description: Unable to add workspace as it already exists
definitions:
Workspace:
title: Workspace
xml:
name: workspace
type: object
properties:
name:
type: string
description: name of the workspace
Swagger 生成器从中创建简单的 类,易于使用:
Workspace workspace = new Workspace();
workspace.setName("test");
WorkspacesApi workspacesApi = new WorkspacesApi(apiClient()); // apiClient() : Authentication
String response = workspacesApi.postWorkspaces(workspace, true);
但是它在调用时产生的内容JSON使服务器失败:
{ name: 'test' }
我发现服务器需要自定义 JSON :
{workspace:{name:'test'}}
并且 Swagger 一代不会响应它的愿望:yaml
包含一个 xml:
属性,它不是 Swagger schema 2.0。该工具不知道如何处理它并忽略它。
definitions:
Workspace:
title: Workspace
xml:
name: workspace
type: object
properties:
name:
type: string
description: name of the workspace
我有办法在 yaml
中输入一些指令来创建服务器正在寻找的自定义 JSON,
将当前生成的 { name: 'test' }
Swagger 生成工具更改为 {workspace:{name:'test'}}
?
或者我是否必须添加一些插件或询问 Swagger 生成工具来覆盖生成的 类(以及如何?)以生成目标自定义 JSON ?
toString()
方法提供的内容是为开发者提供的一些便利功能。它不一定会产生对象的有效 JSON 表示。您应该使用 JSON 序列化程序将 JAVA 对象序列化为 JSON 字符串。例如,这可以在 Gson 库的帮助下完成。
实际上“xml”是 Swagger 2.0 的一部分(参见 https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#xml-modeling),并且在使用 XML 表示时允许对模型进行细粒度控制。
但是您使用的是 JSON,因此由生成的 Swagger 客户端生成的 JSON 负载非常好,并且应该被服务器接受。
这给您带来了两个选择:
a) 通过接受负载修复服务器(100% 符合 API 规范)
b) 编写自定义客户端,将自定义的 JSON 负载发送到服务器。这样生成的Swagger Client不能自定义
@eidottermihi
默认情况下,Swagger 客户端在生成客户端 类.
时接受 JSON
内容
如果我将其 consumes:
声明更改为仅 application/xml
,则在运行时,客户端将抱怨它没有准备好 xml
的处理程序。
问题是你说的那个。编写 yaml
文件的人将其编写为生成 Swagger-UI
文档,但他们是手动完成的,并且 yaml
:
- 不是从服务器 类、 自动创建的
- 或者它不生成服务器实现的基本接口。
这是一个 yaml
写在一边,“我们相信我们的 REST 服务就是这些。”。但是没有保证,没有证据...
因此,无法生成正确的客户端来调用它假装代表的 REST 服务,这毫无用处。
我的问题有答案了:
我能够通过将方法 $ref: "#/definitions/Workspace"
更改为 $ref: "#/definitions/WorkspaceRequest"
来生成对方法 postWorkspaces
的良好 JSON 请求,我已声明:
post:
operationId: postWorkspaces
tags:
- "Workspaces"
summary: add a new workspace to GeoServer
description: Adds a new workspace to the server
security:
- basic: []
parameters:
- name: workspaceBody
description: The layer group body information to upload.
in: body
required: true
schema:
$ref: "#/definitions/WorkspaceRequest"
- name: default
in: query
description: New workspace will be the used as the default. Allowed values are true or false, The default value is false.
required: false
type: boolean
default: false
consumes:
- application/json
- application/xml
produces:
- text/html
- application/json
- application/xml
responses:
201:
description: Created
schema:
type: string
headers:
Location:
description: URL where the newly created workspace can be found
type: string
401:
description: Unable to add workspace as it already exists
definitions:
Workspace:
title: Workspace
xml:
name: workspace
type: object
properties:
name:
type: string
description: name of the workspace
WorkspacesResponse:
title: Workspaces
type: object
properties:
workspaces:
$ref: "#/definitions/WorkspaceResponse"
WorkspaceRequest:
title: Workspace Request
type: object
properties:
workspace:
$ref: "#/definitions/Workspace"