我一直在为springboot项目使用swagger编码。现在,我可以在Java中自动生成客户端代码,而无需考虑数据模型的生成(通过importMapping将自己的代码导入swagger-codegen-maven-plugin)。 此外,我还能够:
以下示例:
@RestController
@RequestMapping(value="/api/external/")
@Tag(name = "addResources", description = "The Add Resources API")
public class ControllerAddResources {
private final ServiceInterAddResources externalSources;
public ControllerAddResources(
ServiceInterAddResources externalSources){
this.externalSources = externalSources;
}
@Operation(operationId="importDataV3", summary = "Import Data V3", description = "Import a Data V3 file from source", tags = { "addResources" })
@PostMapping(path="/{source}/datav3", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public @ResponseBody String importDataV3(
@PathVariable String source,
MultipartFile file) throws ExceptionInvalidDataV3, IOException{
return externalSources.importDataV3(source, file);
}
所有这些均内置于openapi合约中,然后代码生成器将其用于生成客户端库(在这种情况下,我使用resttemplate),例如以下内容:
@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaClientCodegen", date = "2020-03-05T12:15:11.537Z[Europe/Lisbon]")@Component("com.xxx.connector.api.AddResourcesApi")
public class AddResourcesApi {
private ApiClient apiClient;
public AddResourcesApi() {
this(new ApiClient());
}
@Autowired
public AddResourcesApi(ApiClient apiClient) {
this.apiClient = apiClient;
}
public ApiClient getApiClient() {
return apiClient;
}
public void setApiClient(ApiClient apiClient) {
this.apiClient = apiClient;
}
/**
* Import Data V3
* Import a Data V3 file from source
* <p><b>412</b> - default response
* <p><b>409</b> - default response
* <p><b>200</b> - default response
* @param source The source parameter
* @param file The file parameter
* @return String
* @throws RestClientException if an error occurs while attempting to invoke the API
*/
public String importDataV3(String source, File file) throws RestClientException {
Object postBody = null;
// verify the required parameter 'source' is set
if (source == null) {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'source' when calling importDataV3");
}
// create path and map variables
final Map<String, Object> uriVariables = new HashMap<String, Object>();
uriVariables.put("source", source);
String path = UriComponentsBuilder.fromPath("/api/external/{source}/datav3").buildAndExpand(uriVariables).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 (file != null)
formParams.add("file", new FileSystemResource(file));
final String[] accepts = {
"*/*", "application/json"
};
final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
"multipart/form-data"
};
final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);
String[] authNames = new String[] { };
ParameterizedTypeReference<String> returnType = new ParameterizedTypeReference<String>() {};
return apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
}
在生成的源代码中,codegen还包括用于junit测试的支架,如下所示:
/**
* API tests for AddResourcesApi
*/
@Ignore
public class AddResourcesApiTest {
private final AddResourcesApi api = new AddResourcesApi();
/**
* Import Data V2
*
* Import an Data V2 file from source
*
* @throws ApiException
* if the Api call fails
*/
@Test
public void imporDataV2Test() {
String source = null;
File file = null;
String response = api.importDataV2(source, file);
// TODO: test validations
}
/**
* Import Data V3
*
* Import an Data V3 file from source [%source%]
*
* @throws ApiException
* if the Api call fails
*/
@Test
public void importDataV3Test() {
String source = null;
File file = null;
String response = api.importDataV3(source, file);
// TODO: test validations
}
}
但是,验证码为空,符合预期。由于客户端是在CI / CD环境中连续生成的,并被部署到我在内部运行的依赖项管理系统(Artifactory),因此无法实现每次执行代码生成时手动编写测试的目的。
是否有一种方法可以在Springboot项目级别直接使用Java注释(或模板机制)来指定验证代码(或验证条件)?这将允许包括测试在内的全自动客户端库生成。