我正在尝试从包含以下内容的Yaml生成客户端
acceptParam:
name: Accept
type: string
required: true
in: header
description: Accepted Content-type. Should be set to application/json
contentTypeParam:
name: Content-Type
type: string
required: true
in: header
description: Request Content-type. Should be set to application/json
这意味着accept
并且contentType
将出现在生成的方法签名中。
最重要的是,我像这样配置插件
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.18</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/swagger.yaml</inputSpec>
<language>java</language>
<configOptions>
<dateLibrary>joda</dateLibrary>
<localVarPrefix>localVar</localVarPrefix>
</configOptions>
<library>resttemplate</library>
<output>${project.build.directory}/generated-sources</output>
<modelPackage>com.example.client.model</modelPackage>
<apiPackage>com.example.client.api</apiPackage>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
</configuration>
</execution>
</executions>
</plugin>
不过,之后 mvn clean install
我越来越
错误:(130,31)Java:方法中已经定义了变量accept
并且生成的代码包含
public Response authorize(Request body, String accept, ...) {
....
final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
....
}
我尝试了不同版本的插件(从2.3.0到最新版本),在克服了许多其他问题之后,我总是这样结束。
在OpenAPI 2.0中,应该使用和而不是参数来定义Accept
和Content-Type
标头。在OpenAPI 3.0中,这些标头定义为请求/响应媒体类型。consumes
produces
如下更改您的操作定义:
swagger: '2.0'
paths:
/foo:
post:
consumes:
- application/json
produces:
- application/json
...
或者,如果您使用OpenAPI 3.0:
openapi: 3.0.0
paths:
/foo:
post:
requestBody:
content:
application/json: # <----
schema:
...
responses:
'200':
description: ok
content:
application/json: # <----
schema:
...
好吧,这说明了很多,很糟糕,那不是我的API。希望一旦我到达开发人员的位置,他将解决此问题。