1.引入swagger所需要的包
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2.添加swagger配置
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//controller包路径
.apis(RequestHandlerSelectors.basePackage("com.springboot.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("Srping boot swagger") //标题
.description("Spring boot 集成Swagger_2") //描述
.contact("cc") //作者
.version("1.0") //版本
.build();
}
}
3.启动spring boot项目,输入http://localhost:8080/swagger-ui.html
另外,还有一种格式,http://localhost:8080/doc.html
1.引入
<!-- https://mvnrepository.com/artifact/com.github.xiaoymin/swagger-bootstrap-ui -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
2.在SwaggerConfig类上添加注解 @EnableSwaggerBootstrapUI