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

springboot整合swagger2

2019-08-04 21:23 393 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/yuanshangshenghuo/article/details/98474411

一.介绍

swagger,中文是“拽”的意思,是一个功能强大的在线api文档的框架,swagger2中提供了在线文档的查阅与测试功能,使用swgger2能够很容易构建restful风格的api。

二.使用

2.1 maven坐标引入

[code]        <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.2 配置类的书写

[code]@Component
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestAPI(){

return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("springboot 利用swagger 构建api文档")
.description("优雅的rest风格")
.termsOfServiceUrl("www")
.version("1.0")
.build()
)
.select()
.apis(RequestHandlerSelectors.basePackage("com.test"))
.paths(PathSelectors.any())
.build();
}

}

2.3 文档注解详解

@Api:修饰整个类,用于描述controller类

@ApiOOperation:描述类的方法,或者说是一个接口

@ApiParam:单个参数的描述。

@ApiModel:用对象来接收参数

@ApiProperty:使用对象接收参数的时候,描述对象的一个字段

@ApiResponse:http响应的一个描述

@ApiResponses:http响应的整体描述

@ApiIgnore:使用该注解,表示swagger2忽略该接口

@ApiError:发生错误返回的信息

@ApiParamlmplicit :一个请求参数

@ ApiParamsimplicit:多个请求参数

2.4 启动项目访问地址

http://ip:port/swagger-ui.html

 

 

 

 

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