您的位置:首页 > 编程语言 > Java开发

springboot 使用 Swagger2 整合api文档

2020-06-06 05:55 344 查看

1.在父工程的pom.xml中引入相关的Swagger2依赖

<!-- swagger2 配置 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version> </dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.6</version>
</dependency>

2.编写Swagger2类

@Configuration
@EnableSwagger2
public class Swagger2 {
//访问地址(原路径):http:localhost:8088/swagger-ui.html
//修饰后的路径:http:localhost:8088/doc.html
@Bean
public Docket createRestApi() {
return  new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.lzx.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("天天吃货 电商平台")
.contact(new Contact("lzx",
"www.baidu.com",
"xxxxxxxxx@qq.com"))
.description("专门为天天吃货提供的文档API")
.version("1.0.1")
.termsOfServiceUrl("www.baidu.com")
.build();
}

}

3.运行访问
访问地址(原路径):http:localhost:8088/swagger-ui.html
修饰后的路径:http:localhost:8088/doc.html(ui修饰后的界面)

4.在一些方法或属性中添加一些中文注释让别人看得懂.
4-1@ApiIgnore标明Api文档中隐藏该接口(在类名上注释)

4-2@Api注释相应名称(在类名上注释)


4-3@@ApiModel(在实体来上注释)


4-4@ApiModelProperty(在实体类上的属性注释)


4-4@ApiParam(在方法属性上的注释)

5.Swgger2也提供了在线请求调试

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: