我有一个JAX-RS应用程序,它使用Swagger2导出openapi.json:
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正常打开。但它试图要求的“试一试”功能:
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) {
}
}