1:pom文件引入坐标
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
2:编写配置类
填写控制器所在包,例如:com.longdatech.demo.controller
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("控制器所在包"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger构建api文档")
.description("简单优雅的restfun风格")
.termsOfServiceUrl("aaa")
.version("1.0")
.build();
}
}
3:访问
浏览器输入:http://localhost:8080/swagger-ui.html
或者:http://localhost:8080/swagger-ui/#
4:界面截图
如在使用过程中遇到问题,可关注公众号在后台留言