rio*_*rio 7 java swagger spring-boot
我正在尝试将我的Spring Boot版本2.0.1.RELEASE与Swagger集成 .
从这篇博文中 看来,通过添加两个Maven依赖项似乎很容易,一切都应该有效.
所以我在pom中添加了以下依赖项:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
并创建了SwaggerConfigbean:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
return docket;
}
}
Run Code Online (Sandbox Code Playgroud)
在属性文件中,我在尝试使其工作期间最终得到了这3个条目:
spring.application.name=cat-service
management.server.servlet.context-path=/cat-service
server.servlet.contextPath=/cat-service
Run Code Online (Sandbox Code Playgroud)
但最后,访问时
http://localhost:8080/cat-service/api/v2/api-docs
或UI页面
http://localhost:8080/cat-service/swagger-ui.html
我收到一个page not found错误.
我在swagger github页面 和stackoverflow 中的这个问题中发现了这个问题 但是我无法改变我的404错误.
H3A*_*3A7 17
只需使用springdoc-openapi-ui即可。
Maven 依赖项:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.11</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
或者摇篮:
implementation 'org.springdoc:springdoc-openapi-ui:1.6.11'
Run Code Online (Sandbox Code Playgroud)
这就是它的全部内容...不需要注释/配置。干杯!
如果您使用的是spring security,请确保您可以访问以下路径以使其正常工作:
<-- for UI<-- for UI redirects<-- for json docs and openapi configuration<-- for yaml docs欲了解更多信息: https: //www.baeldung.com/spring-rest-openapi-documentation
rio*_*rio 10
我能够使其与Spring boot版本2.0.4.RELEASE和此博客文章一起使用:
我添加了以下依赖项:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
而这个配置文件:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SpringFoxConfig {
@Bean
public Docket apiDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
而且有效。
可以通过/swagger-ui.html#访问Swagger UI。
小智 10
请查看参考:https : //springfox.github.io/springfox/docs/current/
“2.1.3. 从现有 2.x 版本迁移”
您可以从 pom.xml 中删除 springfox-swagger2 和 springfox-swagger-ui 并添加 springfox-boot-starter (例如版本 3.0.0)。您也可以删除@EnableSwagger2 注释
并且:“swagger-ui 位置已从 http://host/context-path/swagger-ui.html 移动到 http://host/context-path/swagger-ui/index.html 或 http://host/简称 context-path/swagger-ui/。这使得它可以更好地将其作为 web jar 拉出来,并在不需要时使用配置属性将其关闭。”
首先在 springboot 文件的同一个包中添加 SwaggerConfig.java 文件,如下例所示。
@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig extends WebMvcConfigurerAdapter {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
Run Code Online (Sandbox Code Playgroud)
试试这个
http://localhost:8080/spring-security-rest/api/swagger-ui.html
或
http://localhost:8080/spring-security-rest/swagger-ui.html
如果这不起作用,请尝试更改 application.properties 中的路径
将此添加到 application.properties:
server.servlet-path=/loop-service
Run Code Online (Sandbox Code Playgroud)
并尝试以下网址:
http://localhost:8080/loop-service/swagger-ui.html (用户界面文档)
http://localhost:8080/loop-service/v2/api-docs (JSON 文档)
小智 5
我也遇到了同样的问题(404 Not Found with springfox 3.0.0)。通过将日志记录级别设置为“DEBUG”,我能够看到端点/v3/api-docs并且它们有效,但没有任何关于“swagger-ui”的信息。
我终于找到了https://github.com/springfox/springfox/issues/3285,它表明:
3.0.0 中的新网址是 /swagger-ui/index.html 或 /swagger-ui/ 而不是 /swagger-ui.html”
难道他们没有添加调试日志来指示 swagger UI 的可用位置吗?
| 归档时间: |
|
| 查看次数: |
9070 次 |
| 最近记录: |