Swagger-UI (Docker) - "Try it out" 时不维护基本路径
Swagger-UI (Docker) - does not maintain base path when "Try it out"
我有一个使用 Swagger2 导出 openapi.json 的 JAX-RS 应用程序:
http://localhost:8080/notification/rest/openapi.json
并公开 OpenAPI:
{
"openapi" : "3.0.1",
"info" : {
"title" : "Notification Module",
"version" : "1.0"
},
"paths" : {
"/sms/send" : {
"post" : {
"operationId" : "send_1",
"requestBody" : {
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/Sms"
}
}
}
},
"responses" : {
"default" : {
"description" : "default response",
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/SendSmsResult"
}
}
}
}
}
}
}
...
}
}
现在我正在尝试在 Docker 容器中展示 Swagger-UI:
docker run -p 80:8080 -e URL="http://localhost:8080/notification/rest/openapi.json" swaggerapi/swagger-ui
Swagger2正常打开。
但是它试图请求的 "Try it out" 功能:
http://localhost:8080/sms/send
缺少上下文和基本路径/notification/rest/
是否可以配置 swagger-ui 以创建 URL 请求:
http://localhost:8080/notification/rest/sms/send
?
已解决。
只需将 servers
配置为 openapi-configuration.yaml
即可。
resourcePackages:
- com.mycompany.app.rest
prettyPrint: true
cacheTTL: 0
buildContext: true
openAPI:
servers:
- url: http://localhost:8080/notification
info:
version: '1.0'
title: Notification Module
此外,添加一个带有 @Context ServletConfig
参数的构造函数很重要:
@ApplicationPath("/rest")
public class App extends Application {
public App(@Context ServletConfig servletConfig) {
}
}
我有一个使用 Swagger2 导出 openapi.json 的 JAX-RS 应用程序:
http://localhost:8080/notification/rest/openapi.json
并公开 OpenAPI:
{
"openapi" : "3.0.1",
"info" : {
"title" : "Notification Module",
"version" : "1.0"
},
"paths" : {
"/sms/send" : {
"post" : {
"operationId" : "send_1",
"requestBody" : {
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/Sms"
}
}
}
},
"responses" : {
"default" : {
"description" : "default response",
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/SendSmsResult"
}
}
}
}
}
}
}
...
}
}
现在我正在尝试在 Docker 容器中展示 Swagger-UI:
docker run -p 80:8080 -e URL="http://localhost:8080/notification/rest/openapi.json" swaggerapi/swagger-ui
Swagger2正常打开。
但是它试图请求的 "Try it out" 功能:
http://localhost:8080/sms/send
缺少上下文和基本路径/notification/rest/
是否可以配置 swagger-ui 以创建 URL 请求:
http://localhost:8080/notification/rest/sms/send
?
已解决。
只需将 servers
配置为 openapi-configuration.yaml
即可。
resourcePackages:
- com.mycompany.app.rest
prettyPrint: true
cacheTTL: 0
buildContext: true
openAPI:
servers:
- url: http://localhost:8080/notification
info:
version: '1.0'
title: Notification Module
此外,添加一个带有 @Context ServletConfig
参数的构造函数很重要:
@ApplicationPath("/rest")
public class App extends Application {
public App(@Context ServletConfig servletConfig) {
}
}