如何在 Spring GET 端点中以编程方式构建和 return OpenApi 3.0 文档?
How to build and return an OpenApi 3.0 Document programmatically in a Spring GET endpoint?
我有一个要求,我需要在我的微服务中有一个 returns 一个 io.swagger.v3.oas.models.OpenAPI
文档的 GET 端点,我想知道如何组成该对象。原始形式的对象如下所示:
{
"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
我试过了,但文档刚刚出来 null/blank:
@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 顺便说一句
根据评论中的讨论,您可以修改此方法,不需要使用WebClient。我必须在我的 swagger 服务中获取文档并使用此代码。你不会 return 一个 OpenAPI 对象,你只是 return 一个字符串,因为你已经有了原始的 json.
getV2SwaggerDoc(新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();
}
我能够通过利用 Spring 的 RestTemplate 而不是 java 的低级 HTTP 库来简化接受的答案
private String retrieveSwaggerJson(String url) {
return new RestTemplate()
.getForEntity(url, String.class)
.getBody();
}
我有一个要求,我需要在我的微服务中有一个 returns 一个 io.swagger.v3.oas.models.OpenAPI
文档的 GET 端点,我想知道如何组成该对象。原始形式的对象如下所示:
{
"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
我试过了,但文档刚刚出来 null/blank:
@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 顺便说一句
根据评论中的讨论,您可以修改此方法,不需要使用WebClient。我必须在我的 swagger 服务中获取文档并使用此代码。你不会 return 一个 OpenAPI 对象,你只是 return 一个字符串,因为你已经有了原始的 json.
getV2SwaggerDoc(新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();
}
我能够通过利用 Spring 的 RestTemplate 而不是 java 的低级 HTTP 库来简化接受的答案
private String retrieveSwaggerJson(String url) {
return new RestTemplate()
.getForEntity(url, String.class)
.getBody();
}