我正在尝试将 Redoc 作为 swagger 的替代方案,以便使用 springboot 2.7 (java 11) 获得更好的个性化文档,在搜索错误原因后,我找到了几个我尝试过的答案:ReDoc 和 Spring boot 的 API 文档(难道我做错了什么 ?) 还可以在 intellij 或导航器中预览文档页面,而无需运行 spring 应用程序,因为该应用程序无法在本地运行。
错误详情:
出了点问题... 获取失败 堆栈跟踪 错误:获取失败 在 t.BaseResolver。 (https://cdn.jsdelivr.net/npm/redoc@latest/bundles/redoc.standalone.js:2:31063) 在 Generator.throw() 处 在s(https://cdn.jsdelivr.net/npm/redoc@latest/bundles/redoc.standalone.js:2:28890)
ReDoc版本:2.1.3 提交:b2d8e0f
应用程序属性:
server.port=4949
springdoc.api-docs.path=/api-docs
spring.web.resources.static-locations=classpath:/custom/
#springdoc.api-docs.enabled=true
#springdoc.swagger-ui.path=/swagger-ui.html
#springdoc.api-docs.path=/api-docs
#springdoc.swagger-ui.enabled=true
#springdoc.swagger-ui.operationsSorter=method
#springdoc.swagger-ui.tryItOutEnabled=true
#springdoc.swagger-ui.filter=false
#springdoc.swagger-ui.tagsSorter=alpha
#springdoc.swagger-ui.validatorUrl=none
#springdoc.swagger-ui.defaultModelRendering=model
#springdoc.swagger-ui.docExpansion=full
我的配置:
@Configuration
@EnableWebMvc
public class Static_ResourceHandler implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/") // Default Static Loaction
.setCachePeriod( 3600 )
.resourceChain(true) //
.addResolver(new GzipResourceResolver())
.addResolver(new PathResourceResolver());
registry
.addResourceHandler("/templates/**")
.addResourceLocations("classpath:/resources/static/");
}
}
我没有添加CSS文件,我只是想让index.html暂时工作(如果有人也可以帮助放置和配置CSS文件,我将不胜感激!)
index.html(资源/静态/index.html)
<!DOCTYPE html>
<html>
<head>
<title>Redoc</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?
family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<redoc spec-url='http://localhost:4949/api-docs'></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc@latest/bundles/redoc.standalone.js">
</script>
</body>
</html>
springboot 依赖:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.2.0</version>
</dependency>
更新:
我正在尝试将 Redoc 与新的 springboot 应用程序(MVC)集成,但收到此错误:
这个配置并没有解决跨源问题
@Configuration
@EnableWebMvc
public class Static_ResourceHandler implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/v3/api-docs")
.allowedOrigins("*")
.allowedMethods("GET") methods as needed
.allowCredentials(true);
}
}
所以我找到了解决方案,不,你不需要运行 Node.js 服务器来让 Redoc 正常工作:
第一步:
你不能允许配置中的所有来源,它只有在你的前端位于同一环境中时才有效:
@Configuration
public class Static_ResourceHandler implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/v3/api-docs")
.allowedOrigins("http://localhost:63342") // changed code
.allowedMethods("GET")
.allowCredentials(true);
}
}
第 2 步:
我将依赖项更改为 openapi-ui,因此不再需要 WebMvcConfigurer :
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.11</version>
</dependency>
第 3 步:
将spec-url更改为swagger url .
<redoc spec-url='http://localhost:4949/v3/api-docs' ></redoc>