maven 依赖
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
</dependency>
import 类和方法
import io.swagger.models.*;
import io.swagger.models.auth.SecuritySchemeDefinition;
import io.swagger.models.parameters.BodyParameter;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.parameters.PathParameter;
import io.swagger.models.parameters.QueryParameter;
import io.swagger.models.properties.*;
具体方法
**
* @param swagger_json_file
* @param JSON 或者 YAML
* @return
*/
public Swagger getSwaggerFromFile(String swagger_json_file) {
JSONObject root = JSONObject.parseObject(swagger_json_file, Feature.OrderedField);
// JSONObject root2 = jsonObject.parseObject(swagger_json_file, JSONObject.class, Feature.OrderedField);
String swagger = root.getString("swagger");
Info info = JsonUtil.parseFormatSort(root.getString("info"), Info.class);
String host = root.getString("host");
String basePath = root.getString("basePath");
List<Tag> tags = new ArrayList<>();
if (!ObjectUtils.isEmpty(root.getJSONArray("tags"))) {
tags = JsonUtil.parseArrFormat(root.getJSONArray("tags").toJSONString(), Tag.class);
}
List<Scheme> schemes = JsonUtil.parseArrFormat(root.getString("schemes"), Scheme.class);
List<String> consumes = JsonUtil.parseArrFormat(root.getString("consumes"), String.class);
List<String> produces = JsonUtil.parseArrFormat(root.getString("produces"), String.class);
List<SecurityRequirement> security = JsonUtil.parseArrFormat(root.getString("security"), SecurityRequirement.class);
//
Map<String, Model> definitions = new LinkedHashMap<>();
//
Map<String, Path> paths = new LinkedHashMap<>();
Map<String, JSONObject> pathsRoot = JsonUtil.parseFormatSort(root.getString("paths"), Map.class);
getPaths(pathsRoot, paths, definitions);
//
Map<String, SecuritySchemeDefinition> securityDefinitions = new LinkedHashMap<>();
//
Map<String, JSONObject> definitionsRoot = JsonUtil.parseFormatSort(root.getString("definitions"), Map.class);
definitions = this.getDefinitionsSwagger(definitionsRoot, definitions);
//
Map<String, Parameter> parameters = new LinkedHashMap<>();
Map<String, JSONObject> parametersRoot = JsonUtil.parseFormatSort(root.getString("parameters"), Map.class);
if (!ObjectUtils.isEmpty(parametersRoot)) {
for (Map.Entry<String, JSONObject> e1 : parametersRoot.entrySet()) {
String mapKey = e1.getKey();
JSONObject mapValue = e1.getValue();
Parameter opMap = JsonUtil.parseFormatSort(mapValue.toJSONString(), Parameter.class);
parameters.put(mapKey, opMap);
}
}
//
Map<String, Response> responses = new LinkedHashMap<>();
Map<String, JSONObject> responsesRoot = JsonUtil.parseFormatSort(root.getString("responses"), Map.class);
if (!ObjectUtils.isEmpty(responsesRoot)) {
for (Map.Entry<String, JSONObject> e1 : responsesRoot.entrySet()) {
String mapKey = e1.getKey();
JSONObject mapValue = e1.getValue();
Response opMap = JsonUtil.parseFormatSort(mapValue.toJSONString(), Response.class);
responses.put(mapKey, opMap);
}
}
//
ExternalDocs externalDocs = JsonUtil.parseFormatSort(root.getString("externalDocs"), ExternalDocs.class);
//
Map<String, Object> vendorExtensions = new LinkedHashMap<>();
Map<String, Object> vendorExtensionsRoot = JsonUtil.parseFormatSort(root.getString("vendorExtensions"), Map.class);
if (!ObjectUtils.isEmpty(vendorExtensionsRoot)) {
for (Map.Entry<String, Object> e1 : vendorExtensionsRoot.entrySet()) {
String mapKey = e1.getKey();
Object mapValue = e1.getValue();
vendorExtensions.put(mapKey, mapValue);
}
}
//
Swagger s = new Swagger();
s.setSwagger(swagger);
s.setInfo(info);
s.setHost(host);
s.setBasePath(basePath);
s.setTags(tags);
s.setSchemes(schemes);
s.setConsumes(consumes);
s.setProduces(produces);
s.setSecurity(security);
s.setPaths(paths);
s.setSecurityDefinitions(securityDefinitions);
s.setDefinitions(definitions);
s.setParameters(parameters);
s.setResponses(responses);
s.setExternalDocs(externalDocs);
s.setVendorExtensions(vendorExtensions);
return s;
}