我有一个要求,我需要在我的微服务中有一个GET端点,该端点返回一个io.swagger.v3.oas.models.OpenAPI
文档,我想知道如何组成该对象。原始形式的对象如下所示:
{
"openapi": "3.0.1",
"info": {
"title": "MY API",
"description": "API for accessing stuff and other stuff.",
"termsOfService": "http://website.com",
"contact": {
"name": "Some chap",
"url": "https://website.com/s/url",
"email": "alwaysReplyAll@office.com"
},
"version": "1.0"
},
"paths": {
"/v1/user/{id}/properties": {
"get": { ...etc etc
ive尝试过此操作,但文档只是空/空白:
@GetMapping("/openapi3")
public @ResponseBody OpenAPI swag() {
OpenAPI swagDoc = new OpenAPI();
GenericOpenApiContextBuilder builder = new GenericOpenApiContextBuilder();
try {
swagDoc = builder.buildContext(true).read();
} catch (OpenApiConfigurationException e) {
// handle error
}
return swagDoc;
}
我已经阅读了有关springfox的文章,但是他们文档中的示例非常清楚……我想知道这是否有必要。我对这个生成器不正确吗?
使用Gradle btw
答案 0 :(得分:0)
根据评论中的讨论,您可以修改此方法,而无需使用WebClient。我必须在swagger服务中获取文档,然后使用此代码。您将不会返回OpenAPI对象,而只会返回一个字符串,因为您已经拥有原始的json。
getV2SwaggerDoc(new URL(“ ...”));
private String getV2SwaggerDoc(URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod(RequestMethod.GET.toString());
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = reader.readLine()) != null)
stringBuffer.append(line);
reader.close();
connection.disconnect();
return stringBuffer.toString();
}
答案 1 :(得分:0)
我能够通过利用Spring的RestTemplate而不是Java的低级HTTP库来简化可接受的答案
private String retrieveSwaggerJson(String url) {
return new RestTemplate()
.getForEntity(url, String.class)
.getBody();
}