我在使用swagger codegen时遇到了一个问题。我已经从swaggerHub自动下载了swagger表单,然后,我使用swagger代码生成客户端。然而,对于POST请求,它需要ContentType作为参数。所以我在编译时收到一条消息:
变量内容类型已在方法验证地址中定义
庞。xml文件:
<!-- download swagger -->
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swaggerhub-maven-plugin</artifactId>
<executions>
<execution>
<id>aec</id>
<phase>generate-sources</phase>
<goals>
<goal>download</goal>
</goals>
<configuration>
<api>Address</api>
<owner>test</owner>
<version>2.13.0</version>
<format>yaml</format>
<token>test</token>
<outputFile>${address-service-swagger.file}</outputFile>
</configuration>
</execution>
</executions>
</plugin>
<!-- generate -->
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<executions>
<execution>
<id>address-service-client</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<ignoreFileOverride>${project.basedir}/.swagger-codegen-ignore</ignoreFileOverride>
<inputSpec>${address-service-swagger.file}</inputSpec>
<language>java</language>
<modelPackage>com.shipment.client.address.pojo</modelPackage>
<apiPackage>com.shipment.client.address.api</apiPackage>
<invokerPackage>com.shipment.client.address.invoker</invokerPackage>
<configOptions>
<generateApis>false</generateApis>
<dateLibrary>java8</dateLibrary>
<sourceFolder>src/gen/java</sourceFolder>
</configOptions>
<library>resttemplate</library>
</configuration>
</execution>
</executions>
</plugin>
swagger文件:
/addresses/validate:
post:
tags:
- Addresses
operationId: validateAddress
description: Validate address
parameters:
- name: Authorization
in: header
required: true
type: string
- name: Content-Type
in: header
description: ...
required: true
type: string
- name: addressRequest
in: body
description: The address request to validation
required: true
schema:
$ref: "#/definitions/AddressRequest"
...
生成的Api类:
public void validateAddress(String authorization, String contentType, AddressRequest addressRequest) throws RestClientException {
Object postBody = addressRequest;
// verify the required parameter 'authorization' is set
if (authorization == null) {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'authorization' when calling validateAddress");
}
// verify the required parameter 'contentType' is set
if (contentType == null) {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'contentType' when calling validateAddress");
}
// verify the required parameter 'addressRequest' is set
if (addressRequest == null) {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'addressRequest' when calling validateAddress");
}
String path = UriComponentsBuilder.fromPath("/addresses/validate").build().toUriString();
final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();
if (authorization != null)
headerParams.add("Authorization", apiClient.parameterToString(authorization));
if (contentType != null)
headerParams.add("Content-Type", apiClient.parameterToString(contentType));
final String[] accepts = { };
final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = { };
final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);
String[] authNames = new String[] { };
ParameterizedTypeReference<Void> returnType = new ParameterizedTypeReference<Void>() {};
apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
}
如何解决这个问题?谢谢!
发生这种情况是因为局部变量名与生成代码中的参数名冲突。添加
<configOptions>
...
<localVarPrefix>localVar</localVarPrefix>
</configOptions>