swagger

-

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
<dependencies>
<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>
</dependencies>

SwaggerConfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
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;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(paths())
.build();
}

// Describe your apis
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger Sample APIs")
.description("This page lists all the rest apis for Swagger Sample App.")
.version("1.0-SNAPSHOT")
.build();
}

// Only select apis that matches the given Predicates.
private Predicate<String> paths() {
// Match all paths except /error
return Predicates.and(
PathSelectors.regex("/.*"),
Predicates.not(PathSelectors.regex("/error.*"))
);
}
}

根据配置启用/关闭 swagger

  • 配置文件:swagger.enable: false
  • swaggerconfig.java:@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")

常用注解

@Api(tags = “行业分析”)

@ApiImplicitParam

  • name - 参数名

  • value - 参数的具体意义,作用

  • required - 参数是否必填

  • dataType - 参数的数据类型

  • paramType - 查询参数类型,这里有几种形式

    • path 以地址的形式提交数据
    • query 直接跟参数完成自动映射赋值
    • body 以流的形式提交 仅支持 POST
    • header 参数在 request headers 里边提交
    • form 以 form 表单的形式提交 仅支持 POST
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Api(tags = "行业分析")

//--

@ApiOperation(value = "3 区域分析下钻")
@ApiImplicitParams({
@ApiImplicitParam(name = "market", value = "market", defaultValue = "ios", allowableValues = "ios,google-play", paramType = "query", dataType = "String"), //
@ApiImplicitParam(name = "category", value = "游戏品类", defaultValue = "Card", paramType = "query", dataType = "String"), //
@ApiImplicitParam(name = "games", value = "全部游戏/新游戏", defaultValue = "all", allowableValues = "all,new", paramType = "query", dataType = "String"), //
@ApiImplicitParam(name = "feeds", value = "下载/收入", allowableValues = "download,revenue", defaultValue = "revenue", paramType = "query", dataType = "String"), //
@ApiImplicitParam(name = "sort_by", value = "增长值/绝对值", allowableValues = "changeInValue,value", defaultValue = "value", paramType = "query", dataType = "String"), //
@ApiImplicitParam(name = "start_date", value = "开始日期", defaultValue = "2020-09-01", paramType = "query", dataType = "String"), //
@ApiImplicitParam(name = "end_date", value = "结束日期", defaultValue = "2020-09-01", paramType = "query", dataType = "String"), //
@ApiImplicitParam(name = "region", value = "地区", defaultValue = "CN", paramType = "query", dataType = "String"),//
})

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×