-
pom文件添加依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
-
注意*
springboot版本2.3.5,swagger版本为2.2.2会报错,升级swagger版本到2.9.2即可。
-
添加配置类
@EnableSwagger2
@Configuration
public class SwaggerConfig{
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.demo.swagger2"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("RESTful API")
.description("demo")
.version("1.0")
.build();
}
}
-
访问localhost:port/swagger-ui.html(port默认8080)