52 lines
2.0 KiB
Java
52 lines
2.0 KiB
Java
package com.guwan.backend.swagger;
|
|
|
|
import io.swagger.v3.oas.models.OpenAPI;
|
|
import io.swagger.v3.oas.models.PathItem;
|
|
import io.swagger.v3.oas.models.Operation;
|
|
import io.swagger.v3.oas.models.media.Schema;
|
|
import io.swagger.v3.parser.OpenAPIV3Parser;
|
|
|
|
import java.util.Map;
|
|
|
|
public class SwaggerAPIParser {
|
|
|
|
private static final String SWAGGER_URL = "http://127.0.0.1:8084/v3/api-docs";
|
|
|
|
public static void parseAPI() {
|
|
OpenAPI openAPI = new OpenAPIV3Parser().read(SWAGGER_URL);
|
|
|
|
|
|
System.out.println("openAPI = " + openAPI);
|
|
|
|
/* openAPI.getPaths().forEach((path, pathItem) -> {
|
|
for (PathItem.HttpMethod method : pathItem.readOperationsMap().keySet()) {
|
|
Operation operation = pathItem.readOperationsMap().get(method);
|
|
|
|
System.out.println("接口:" + method + " " + path);
|
|
|
|
// 解析 Query & Path 参数
|
|
Map<String, String> queryParams = SwaggerQueryParamParser.extractQueryParameters(operation.getParameters());
|
|
queryParams.forEach((key, value) -> System.out.println(" - 参数:" + key + " 类型:" + value));
|
|
|
|
// 解析请求体
|
|
if (operation.getRequestBody() != null) {
|
|
Schema<?> bodySchema = SwaggerRequestBodyParser.extractRequestBodySchema(operation.getRequestBody(), openAPI);
|
|
System.out.println(" - 请求体:" + bodySchema.getProperties());
|
|
}
|
|
|
|
// 解析响应体
|
|
Schema<?> responseSchema = SwaggerResponseParser.extractResponseSchema(operation.getResponses(), openAPI);
|
|
if (responseSchema != null) {
|
|
System.out.println(" - 响应数据:" + responseSchema.getProperties());
|
|
}
|
|
|
|
System.out.println("=================================");
|
|
}
|
|
});*/
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
parseAPI();
|
|
}
|
|
}
|