使用swagger-codegen时出现问题。我已经自动下载了swaggerHub形式的swagger表格,然后使用swagger-codegen生成了客户端。但是,对于POST请求,需要使用Content-Type作为参数。因此,在编译时会收到一条消息:
variablecontentType已经在方法validateAddress中定义
Pom.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);
}
如何解决这个问题?谢谢!
答案 0 :(得分:0)
发生这种情况是因为本地变量名称与生成的代码中的参数名称冲突。将<localVarPrefix>
添加到<configOptions>
可以避免这种冲突。
<configOptions>
...
<localVarPrefix>localVar</localVarPrefix>
</configOptions>