我正在使用 yaml 文件来控制项目的 swagger 页面上的内容。我几乎可以让所有东西看起来都正确,除了我想让顶部栏有“选择定义”下拉菜单(或者没有任何内容,如果现在更简单的话)而不是“探索”控件。到目前为止,我已经看到了如何实现这一点的零碎内容,但还没有从头开始完成。
\n\n\n\n问题是如何使用自定义 Swagger YML 文件在 Spring Boot 中创建自定义布局?看来你需要使用 React 创建一个自定义布局,我对此一无所知。一些进行自定义布局的示例非常简单。但是,我不知道将带有 React 代码的文件放在哪里。此外,一些示例表明 ok\xe2\x80\xa6yes 您需要通过指向 application.yml 文件中的自定义布局来配置它,如下所示:
\n\n我不知道以 aka MyLayout.js .jsx 或其他内容为后缀的 React 文件类型是什么。\n我也不知道在 Spring Boot 项目中的何处放置此自定义布局文件。\n我要编译它吗?如果是这样怎么办?
\n我正在运行express/node 应用程序并使用"swagger-ui-express": "^4.5.0",. 我已经设置了一个要求,即需要将jsonwebtoken不记名令牌与所有请求一起发送到我的 api 中的任何端点。
我已经加载了 swagger 文档并正常工作,但现在当试图弄清楚如何将其传递Authorization: Bearer <token>到我的所有端点时,它似乎不起作用。我可以添加securitySchemes+ 子选项,并在我的 swagger 文档中获得绿色Authorize按钮,但是当我输入不记名令牌并发送请求时,加载旋转器会继续旋转并且从不发送请求。我morgan在我的应用程序中设置了日志记录,因此我可以看到对我的端点的请求永远不会被发送或记录。
如何向从 swagger UI 发送的请求发送不记名令牌?
在 app.js 中,我有一条可以在 localhost 中正确加载的路由
// Single entry point for swagger docs
router.use(
'/swaggerDocs',
swaggerDoc.serve,
swaggerDoc.setup(swaggerDocumentation),
);
Run Code Online (Sandbox Code Playgroud)
swaggerDocumentation来自上面的代码片段(配置文件)。
import getCountryRegions from './getCountryRegions.doc.js';
export default {
openapi: '3.0.3',
info: {
title: 'Node/express rest api app',
version: '0.0.1',
},
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
in: 'header', …Run Code Online (Sandbox Code Playgroud) 我在 Firebase Cloud Functions 上开发了一个 API,我想包含它的文档路径。我正在使用 swagger,我可以在本地成功测试它 (localhost:PORT/docs),但是当我将该函数部署到 Firebase 时它不起作用,它会将我重定向到授权页面。
我想我明白了这是为什么:
假设我的云函数的名称是 cfunc。那么它的基本 URL 类似于https://region-name-project-name.cloudfunctions.net/cfunc。基于我如何包含 swagger 文档:
const swaggerDoc = require('./docs/swagger.config.json')
app.use(
'/docs',
allowCors,
swaggerUi.serve,
swaggerUi.setup(swaggerDoc, {
customCssUrl: '/assets/swagger.css',
customSiteTitle: 'My Function Title',
customfavIcon: '/assets/logo.ico',
swaggerOptions: {
supportedSubmitMethods: [] //to disable the "Try it out" button
}
})
)
Run Code Online (Sandbox Code Playgroud)
文档应位于https://region-name-project-name.cloudfunctions.net/cfunc/docs。当我尝试访问该 URL 时,在浏览器 DevTools 中观看“网络”,它尝试在该 URL 上执行 GET 响应 304,然后重定向到https://region-name-project-name.cloudfunctions.net/docs,这就是是什么调出了 Google 身份验证页面,因为没有名为“docs”的云函数,所以 Google 认为我正在尝试访问 Firebase 云函数中的其他内容(如果我执行类似https://region-name- 的操作,也会发生同样的情况)项目名称.cloudfunctions.net/tomato)
但我仍然不知道如何修复此重定向或为什么会发生这种情况。我尝试将 Cloud Function URL 添加到 swagger.config.json …
该代码在 Postman 中运行良好,并提供有效的响应,但无法生成 OpenAPI/Swagger UI 自动文档。
class Role(str, Enum):
Internal = "internal"
External = "external"
class Info(BaseModel):
id: int
role: Role
class AppInfo(Info):
info: str
@app.post("/api/v1/create", status_code=status.HTTP_200_OK)
async def create(info: Info, apikey: Union[str, None] = Header(str)):
if info:
alias1 = AppInfo(info="Portal Gun", id=123, role=info.role)
alias2 = AppInfo(info="Plumbus", id=123, , role=info.role)
info_dict.append(alias1.dict())
info_dict.append(alias2.dict())
return {"data": info_dict}
else:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Please provide the input"
)
Run Code Online (Sandbox Code Playgroud)
收到错误:
TypeError: Object of type 'type' is not JSON serializable
Run Code Online (Sandbox Code Playgroud) 我使用 swagger 2 创建了一个 Spring Boot 3 的示例。
运行应用程序后,我尝试通过此 URL ( http://localhost:8080/swagger-ui.html) 打开 swagger ui,但得到“Whitelabel Error Page”结果。
下面是 swagger 配置
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("com.deprem"))
.paths(PathSelectors.regex("/.*"))
.build().apiInfo(apiEndPointsInfo());
}
private ApiInfo apiEndPointsInfo() {
return new ApiInfoBuilder().title("Title")
.description("Description")
.contact(new Contact("Name and Surname", "", ""))
.license("Apache 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
.version("1.0.0")
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
以下是pom.xml中用于swagger ui的依赖项
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope> …Run Code Online (Sandbox Code Playgroud) 我在 Spring Boot 中通过 Openapi 打开 swagger-ui 时遇到问题。
当我尝试打开此 URL http://localhost:8080/swagger-ui.html 时,我收到Whitelabel 错误页面
我该如何解决这个问题?
这是pom.xml中定义的依赖项,如下所示。
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这是如下所示的openpi 配置类。
@Configuration
public class OpenApiConfig {
@Bean
public OpenAPI customOpenAPI(@Value("${application-description}") String description,
@Value("${application-version}") String version) {
return new OpenAPI()
.info(new Info().title("API")
.version(version)
.description(description)
.license(new License().name("API Licence")));
}
}
Run Code Online (Sandbox Code Playgroud)
这是如下所示的 application.properties 文件。
springdoc.swagger-ui.path=/swagger-ui.html
application-description=API Description
application-version=1.0
logging.level.org.springframework.web=DEBUG
logging.level.io.springfox=DEBUG
Run Code Online (Sandbox Code Playgroud)
当我尝试打开此 URL 时,出现以下错误http://localhost:8080/swagger-ui.html
2023-02-09T08:36:16.593+03:00 DEBUG 20184 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : GET "/swagger-ui.html", parameters={}
2023-02-09T08:36:16.594+03:00 …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的项目中使用最少的 API。我面临的问题是传统控制器的方式是 swagger 为每个控制器生成一个单独的部分。但我看不到在 Minimal API 中添加这种分离的选项。是否可以使用最少的 API 在 Swagger 中添加不同的部分?
我按照以下方式定义我的回复:
responses:
200:
description: 'An authProvider object'
schema:
type: 'array'
items:
$ref: '#/definitions/AuthProvider'
Run Code Online (Sandbox Code Playgroud)
我可以在响应中直接附加另一个字段或2吗?我想补充一些我不想加入的内容AuthProvider
是否可以像在redoc API页面的swagger.io html页面中那样提出请求?或者redoc只是为了显示API的细节?
当我用弹簧靴将swagger2更新为swagger2时,它停止显示pageable应该显示的类型的 正确参数page,size而是开始显示pageSize,pageNumber而在其余方面不正确。
我没有手动更改任何内容,但是由于某种原因,它显示了错误的参数名称。
return new Docket(DocumentationType.SWAGGER_2)
.groupName("Rest API")
.securitySchemes(Collections.singletonList(new BasicAuth(BASIC_AUTH)))
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.paths(s -> oneOf(
"/some/**",
"/search-controller/**").test(s))
.build();
Run Code Online (Sandbox Code Playgroud)
Pom是
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-data-rest</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
控制器如下所示
@RequestMapping(method = RequestMethod.GET)
public HttpEntity<?> findAll(@RequestParam(value = "countryIsoAlpha2", required = false) final String countryKey, final Pageable pageable){
}
Run Code Online (Sandbox Code Playgroud) swagger-ui ×10
swagger ×6
spring-boot ×4
openapi ×3
java ×2
node.js ×2
asp.net-core ×1
c# ×1
fastapi ×1
firebase ×1
minimal-apis ×1
python ×1
spring ×1
springdoc ×1
swagger-2.0 ×1