Spring Boot 2.6.3 与 Springdoc。
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.5</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
在 中applicaton.yaml
,当我将路径设置为 /v3/api-docs 或将其删除时,这意味着使用默认路径“/v3/api-docs”。Swagger UI 页面使用 API http://localhost:8080/swagger-ui/index.html 正确显示
但我想重写路径如下
api-docs.path: /bus/v3/api-docs
Run Code Online (Sandbox Code Playgroud)
然后 Swagger UI 显示“无法加载远程配置”错误:
我正在尝试使用 Spring Boot 2.6.7 运行 Springdoc。
配置:
@Configuration
public class SwaggerConfiguration {
@Bean
public GroupedOpenApi publicApi() {
return GroupedOpenApi.builder()
.group("springshop-public")
.pathsToMatch("/public/**")
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
错误堆栈:
00:50:54.956 [main] ERROR SpringApplication[reportFailure:830] - Application run failed
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()" because "this.condition" is null
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181)
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)
at java.base/java.lang.Iterable.forEach(Iterable.java:75)
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155)
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
at com.atlas.psp.AtlasRouterApplication.main(AtlasRouterApplication.java:53) …
Run Code Online (Sandbox Code Playgroud) https://www.dariawan.com/tutorials/spring/documenting-spring-boot-rest-api-springdoc-openapi-3/
试图遵循这些
我如何处理像这样的注释
@ApiModel(value = "Response container")
@ApiModelProperty(value = "Iventory response", required = true)
Run Code Online (Sandbox Code Playgroud) 如何在springdoc-openapi-ui (OpenAPI 3.0 /swagger-ui.html
) 中为不记名令牌认证启用“授权”按钮,例如 JWT。
Spring@Controller
和@Configuration
类需要添加哪些注解?
为了在 SpringBoot 2.7 中启用 Swagger,选择 springdoc-openapi 相对于 Springfox 的优点/缺点是什么
当我发布时,我会使用HTTPS请求而不是HTTP,但是swagger原始URL仍然是HTTP,我不知道如何设置,并且原始springdoc-openapi-ui配置中没有服务器的文档
我无法让它发挥作用。我已经从一个工作的 SpringBoot (v2.2.2) 应用程序和一个工作的 @RestController 开始。为了让 springdoc 工作,我在我的 pom 中包含了以下依赖项:
\n\n <dependency>\n <groupId>org.springdoc</groupId>\n <artifactId>springdoc-openapi-ui</artifactId>\n <version>1.2.28</version>\n</dependency>\n
Run Code Online (Sandbox Code Playgroud)\n\n然后我构建并开始了我的服务。然后我浏览到 api-docs 端点,得到:
\n\n{"openapi":"3.0.1","info":{"title":"OpenAPI definition","version":"v0"},"servers":[{"url":"http://localhost:8081","description":"Generated server url"}],"paths":{},"components":{}}\n
Run Code Online (Sandbox Code Playgroud)\n\n我是不是少了一步?我假设在运行时它会扫描我的代码,找到 @RestController,找到 @RequestMapping 方法并从中生成 api。
\n\n作为第二次尝试,我决定使用 swagger @Operation 注释来注释我的方法之一,但仍然没有被拾取。
\n\n最后,我尝试使用 springdoc.packagesToScan 手动指定控制器的包,但这也不起作用。
\n\n这感觉就像 springdoc 没有看到我的控制器..即使 spring 看到了..它完全活跃并且可以工作..
\n\n不知道我错过了什么..
\n\n这是我的项目依赖项:
\n\n[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.2.2.RELEASE:compile\n[INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.2.2.RELEASE:compile\n[INFO] | | +- org.springframework.boot:spring-boot:jar:2.2.2.RELEASE:compile\n[INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.2.2.RELEASE:compile\n[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.2.2.RELEASE:compile\n[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile\n[INFO] | | | | \\- ch.qos.logback:logback-core:jar:1.2.3:compile\n[INFO] | …
Run Code Online (Sandbox Code Playgroud) 我SecurityScheme
对 java SpringBoot RESTful 应用程序使用 springdoc-openapi有以下定义:
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.components(new Components().addSecuritySchemes("bearer-jwt",
new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT")
.in(SecurityScheme.In.HEADER).name("Authorization")))
.info(new Info().title("App API").version("snapshot"));
}
Run Code Online (Sandbox Code Playgroud)
是否可以将其全局应用于所有路径,而不必在代码中的任何地方添加@SecurityRequirement
注释@Operation
?
如果是,如何向不安全的路径添加排除项?
我正在将 swagger UI 添加到我的 Spring boot 应用程序中。当我尝试访问 swagger-ui.html 时。我收到 404 错误。
配置类:
@Configuration
public class SwaggerConfig {
@Bean
public OpenAPI springShopOpenAPI() {
return new OpenAPI()
.info(new Info().title("JOYAS-STOCK API Docs")
.description("JOYAS-STOCK REST API documentation")
.version("v1.0.0"));
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序属性:
#swagger-ui config
springdoc.swagger-ui.path=/swagger-ui
springdoc.swagger-ui.operationsSorter=method
springdoc.swagger-ui.tagsSorter=alpha
Run Code Online (Sandbox Code Playgroud)
pom.xml:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.13</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
错误消息:Whitelabel 错误页面 此应用程序没有 /error 的显式映射,因此您将其视为后备。
出现意外错误(类型=未找到,状态=404)。
我开始实施 swagger 的配置,显然它不起作用。
我阅读了这个https://springdoc.github.io/springdoc-openapi-demos/文档以使用 springdoc-openapi-webflux-ui。正如文档所说,我刚刚springdoc-openapi-webflux-ui
在我的应用程序中添加了库:implementation('org.springdoc:springdoc-openapi-webflux-ui:1.2.26')
此外,我在 application.yml 中自定义了 API 的路径:
springdoc:
swagger-ui:
path: /swagger-ui.html
Run Code Online (Sandbox Code Playgroud)
当我启动应用程序并转到http://localhost:8080/swagger-ui.html 时,它会将我重定向到http://localhost:8080/webjars/swagger-ui/index.html?configUrl=/v3/api -docs/swagger-config。在该页面中,我收到一个错误:
Whitelabel Error Page
This application has no configured error view, so you are seeing this as a fallback.
Mon Jan 20 05:16:10 UTC 2020
[7192d9dc] There was an unexpected error (type=Not Found, status=404).
No matching handler
Run Code Online (Sandbox Code Playgroud)
问题是:我应该向我的应用程序添加其他配置以显示 API 文档吗?
PS:我使用 spring.boot 2.2.2:RELEASE
springdoc ×10
spring-boot ×6
java ×5
openapi ×5
spring ×4
swagger-ui ×4
swagger ×3
jwt ×1
springfox ×1
swagger-2.0 ×1