springboot整合swagger-ui

 Swagger-UI可以动态地根据注解生成在线API文档。

 

在pom.xml中新增Swagger-UI相关依赖

<!--Swagger-UI API文档生产工具-->
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.7.0</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.7.0</version>
</dependency>

 

添加Swagger-UI的Java配置文件

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger2API文档的配置
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为当前包下controller生成API文档
                .apis(RequestHandlerSelectors.basePackage("com.xc.mall.controller"))
                //为有@Api注解的Controller生成API文档
//                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                //为有@ApiOperation注解的方法生成API文档
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SwaggerUI演示")
                .description("mall项目")
                .contact("xc")
                .version("1.0")
                .build();
    }
}

 

常用注解

  • @Api:用于修饰Controller类,生成Controller相关文档信息
  • @ApiOperation:用于修饰Controller类中的方法,生成接口方法相关文档信息
  • @ApiParam:用于修饰接口中的参数,生成接口参数相关文档信息
  • @ApiModelProperty:用于修饰实体类的属性,当实体类是请求参数或返回结果时,直接生成相关文档信息

1. Api

@Api用在接口文档资源类上,用于标记当前类为Swagger的文档资源。其中含有几个常用属性,分别说明如下。

• value:定义当前接口文档的名称。

• description:用于定义当前接口文档的介绍。

• tag:可以使用多个名称来定义文档,但若同时存在tag属性和value属性,则value属性会失效。

• hidden:如果值为true,就会隐藏文档。

2. ApiOperation

@ApiOperation用在接口文档的方法上,主要用来注解接口。其中包含几个常用属性,分别说明如下。

• value:对API的简短描述。

• note:API的有关细节描述。

• tags:该属性就是对实现相同业务场景的接口做一个分组。

• hidden:如果值为true,就会在文档中隐藏。

3. ApiResponse、ApiResponses

@ApiResponses和@ApiResponse二者配合使用返回HTTP状态码。@ApiResponses的value值是@ApiResponse的集合,多个@ApiResponse用逗号分隔。其中,@ApiResponse包含的属性如下。

• code:HTTP状态码。

• message:HTTP状态信息。

• responseHeaders:HTTP响应头。

4. ApiParam

@ApiParam用于方法的参数,其中包含以下几个常用属性。

• name:参数的名称。

• value:参数值。

• required:如果值为true,就是必传字段。

• defaultValue:参数的默认值。

• type:参数的类型。

• hidden:如果值为true,就隐藏这个参数。

5. ApiImplicitParam、ApiImplicitParams

二者配合使用在API方法上,@ApiImplicitParams的子集是@ApiImplicitParam注解,其中@ApiImplicitParam注解包含以下几个参数。

• name:参数的名称。

• value:参数值。

• required:如果值为true,就是必传字段。

• defaultValue:参数的默认值。

• dataType:数据的类型。

• hidden:如果值为true,就隐藏这个参数。

• allowMultiple:是否允许重复。

6. ResponseHeader

API文档的响应头,如果需要设置响应头,就将@ResponseHeader设置到@ApiResponse的responseHeaders参数中。@ResponseHeader提供了以下几个参数。

• name:响应头名称。

• description:响应头备注。

7. ApiModel

设置API响应的实体类,用作API返回对象。@ApiModel提供了以下几个参数。

• value:实体类名称。

• description:实体类描述。

• subTypes:子类的类型。

8. ApiModelProperty

设置API响应实体的属性,其中包含以下几个参数。

• name:属性名称。

• value:属性值。

• notes:属性的注释。

• dataType:数据的类型。

• required:如果值为true,就必须传入这个字段。

• hidden:如果值为true,就隐藏这个字段。

• readOnly:如果值为true,字段就是只读的。

• allowEmptyValue:如果为true,就允许为空值。

 

 

参考:

https://macrozheng.github.io/mall-learning/#/architect/mall_arch_02

Spring Boot 2实战之旅 11.9 使用Swagger构建接口文档

https://github.com/swagger-api/swagger-core

posted @ 2019-08-30 14:40  草木物语  阅读(335)  评论(0编辑  收藏  举报