尝试在 Spring Boot 2.3.1 中配置 swagger。
Gradle 配置
repositories {
mavenCentral()
maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local/' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation "io.springfox:springfox-boot-starter:3.0.0-SNAPSHOT"
compile('io.springfox:springfox-swagger2:3.0.0-SNAPSHOT')
compile('io.springfox:springfox-swagger-ui:3.0.0-SNAPSHOT')
}
Swagger 配置
@Configuration
@EnableSwagger2
public class ApplicationSwaggerConfig {
@Bean
public Docket employeeApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(getApiInfo());
}
//create api metadata that goes at the top of the generated page
private ApiInfo getApiInfo() {
return new ApiInfoBuilder()
.title("Employee API")
.version("1.0")
.description("API for managing employees.")
.contact(new Contact("Craig Golightly", "http://globomantics.com", "[email protected]"))
.license("Apache License Version 2.0")
.build();
}
}
控制器
@RestController
public class TestController {
@RequestMapping(value = "/HelloWorld", method = RequestMethod.GET)
public String HelloWorld(){
return "Hello World";
}
}
申请
@SpringBootApplication
public class MeroRentalRestApiApplication {
public static void main(String[] args) {
SpringApplication.run(MeroRentalRestApiApplication.class, args);
}
}
错误
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Jul 06 21:19:55 AEST 2020
There was an unexpected error (type=Not Found, status=404).
能够解决问题
删除以下依赖项
compile('io.springfox:springfox-swagger2:3.0.0-SNAPSHOT')
compile('io.springfox:springfox-swagger-ui:3.0.0-SNAPSHOT')
删除 swagger 2 注释
@EnableSwagger2
导航 URL 为 http://localhost:8080/swagger-ui/index.html
在 Spring Boot 中,只需添加此即可工作,不需要其他依赖项:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
网址是/swagger-ui/
在我的 pom.xml 中添加这 2 个依赖项解决了问题
<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>
希望它可以帮助某人
如果您使用的是 swagger 2,在这种情况下,网址会更新。
Old url :
http://localhost:8080/swagger-ui.html/
New url :
http://localhost:8080/swagger-ui/index.html
如果您使用的是 Spring Boot,请将这两个依赖项添加到 swagger 2 的 pom 中。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
和
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
参考:https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
在一些旧版本的 openapi-ui 中(在我的例子中版本是 1.6.5),应该在配置文件中启用 swagger-ui,即 application.yml:
...
springdoc:
swagger-ui:
enabled: true