1.下载swagger2依赖
swagger2
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
2.下载swagger-bootstrap-ui依赖
swagger-bootstrap-ui
<!-- https://mvnrepository.com/artifact/com.github.xiaoymin/swagger-bootstrap-ui -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.3</version>
</dependency>
3.在项目中pom文件中加入依赖
4.配置SwaggerConfig文件
必须使用以下配置开启swagger服务
@EnableSwagger2
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("接口列表")
.apiInfo(apiInfo())
.select()
//包下的类,才生成接口文档
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
// .paths(PathSelectors.ant("/file/**"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("文档")
.description("文件服务器文档")
.termsOfServiceUrl("https://xx.xxx")
.version("1.1")
.build();
}
}
@Api(tags = "文件服务器接口列表")
6.在controller方法中添加方法接口说明
@ApiOperation("上传文件")
7.启动项目后打开页面
当前我的项目 使用的端口为8102 。项目path为 /fileserver
所以我使用路径为下
http://localhost:8102/fileserver/doc.html
http://localhost:port/项目名称(配置,如无配置则不需要)/doc.html
配置完成
8.其他参数配置:
swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数