spring boot集成swagger2
程序员文章站
2022-07-03 08:42:55
...
一.先导入依赖
<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>
二.新增swagger2配置文件
@Configuration
@EnableSwagger2
public class Swagger2 {
/**
* 要扫描的包
*/
public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.demo.springboot.controller";
/**
* 版本
*/
public static final String VERSION = "1.0.0";
/**
* swagger标题
*/
public static final String TITLE = "试一试swagger2";
/**
* swagger描述
*/
public static final String DESCRIPTION = "spring boot swagger";
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(TITLE)
.description(DESCRIPTION)
.version(VERSION)
.build();
}
}
三.重启项目,打开浏览器输入 http://localhost:8080/swagger-ui.html 查看效果
wagger2集成成功,之后就可以愉快的用swagger2调试代码了