1.spring-boot依赖
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
  | 
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
  | 
 
2.swagger 配置
swagger文档默认访问地址:/swagger-ui.html
swagger文档默认访问地址:/swagger-ui.html
 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
45
46
47
48
49
  | 
@Configuration
@EnableSwagger2 //开启swagger,自动生成文档
public class SwaggerConfig {
    //一个Docket是一个组,组的划分可以通过paths()或apis()来实现,组名称可以作为api搜索条件。组包含多个tags,tag可以包含多个api;
    @Bean
    public Docket api() {
        //apiInfo():配置文档描述
        //apis():api过滤规则,可以自定义为那些接口生成文档
        //useDefaultResponseMessages():是否使用默认的响应信息
        //paths()::可以根据请求路径过滤api
        //globalResponseMessage()::所有接口统一定义响应信息
        //produces(): 为所有接口设置响应类型
        //consumes():(): 为所有接口设置请求类型
        //protocols(): 为所有接口设置支持的协议
        //groupName():组名称
        //tags:设置标签
        Tag tag = new Tag("订单导出api", "订单相关导出api描述");
        ResponseMessage error = new ResponseMessageBuilder().code(500).message("系统异常").build();
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.GET, Lists.newArrayList(error))
                .apiInfo(apiInfo())
                .groupName("订单导出api")
                .protocols(Sets.newHashSet("http", "https"))
                .consumes(Sets.newHashSet(MediaType.APPLICATION_JSON_VALUE))
                .produces(Sets.newHashSet(MediaType.APPLICATION_JSON_VALUE))
                .tags(tag)
                .select()
                .apis((RequestHandlerSelectors.basePackage("com.zhen.export"))
                        .paths(PathSelectors.any()).build();
    }
    private ApiInfo apiInfo() {
        //title:文档标题
        //description:文档的描述
        //termsOfServiceUrl:文档的服务条款说明网址
        //version:版本号
        //contact:文档的联系人信息
        //license:文档版权信息
        //licenseUrl:版权信息超链接
        return new ApiInfoBuilder()
                .title("珍品导出系统API文档")
                .description("珍品导出系统API文档")
                .contact(new Contact("moon", "www.baidu.com", "moon@zhen.com"))
                .license("license-moon").
        .licenseUrl:
        版权信息超链接("license-moon-url")
                .termsOfServiceUrl("http://www.zhen.com").version("2.0").build();
    }
  | 
 
3.相关注解使用和说明
3.相关注解使用和说明
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
  | 
@RestController
@RequestMapping("/orders")
@Api(tags = "订单导出api")
public class OrderExportController {
    @ApiOperation(value = " 订单导出任务-对象请求", notes = "订单导出任务详细描述", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "test", value = "测试字段", required = true, dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "系统异常")
    })
    @RequestMapping(value = "/doExportTaskToOperationOnObject.json", method = RequestMethod.GET)
    public EgResponse doExportTaskToOperationOnObject(OrderDetailRequest req,
                                                      @ApiIgnore String test, HttpServletRequest request) throws Exception {
        return EgResponse.success(new OrderInfo());
    }
  | 
 
- OrderDetailRequest:接口的请求对象,不需要格外注解,可以自动识别展示在文档中,可以用ApiModelProperty注解定义对象字段在文档中的展示
 
- @ApiIgnore:可以用在controller类,方法参数上,使其不展示在文档中
 
- @api:为功能模块添加说明信息,不是必须的,所有匹配的controller都会自动生成文档
 
| 属性名 | 
说明 | 
| tags | 
接口的标签名,可用于分组,不能设置tag的描述,只能在swagger配置时设置组描述。每个controller都会生成默认的标签 | 
| produces | 
支持的响应类型,mime类型,多个值逗号分隔 | 
| consumes | 
入参类型值,mime类型,多个值逗号分隔 | 
| authorizations | 
获取此操作的授权列表 | 
| protocols | 
支持的传输协议,如http。多个协议逗号分隔 | 
| hidden | 
是否隐藏该模块 | 
- @ApiImplicitParams:描述方法参数的集合包含多个ApiImplicitParam注解
 
- @ApiImplicitParam:描述方法参数信息
 
| 属性名 | 
说明 | 
| value | 
参数描述 | 
| name | 
参数名称 | 
| defaultValue | 
参数默认值 | 
| allowableValues | 
参数允许的取值集合 | 
| required | 
是否必填 | 
| allowMultiple | 
是否可以传多个 | 
| dataType | 
参数值的类型 | 
| dataTypeClass | 
参数值得java类型 | 
| paramType | 
参数的类型,可选值path, query, body, header,form | 
| example | 
参数值例子,paramType!=body时有效 | 
| examples | 
paramType=body时有效 | 
| allowEmptyValue | 
是否可以传递空值 | 
- @ApiOperation:模块中每个接口定义,不是必须的,spring会自动扫描所有的接口。
 
| 属性名 | 
说明 | 
| value | 
接口名称 | 
| httpMethod | 
接口使用http方法,可选值 “GET”, “HEAD”, “POST”, “PUT”, “DELETE”, “OPTIONS”,“PATCH”。如果未指定则使用除"PATH"之外的其它所有 | 
| produces | 
支持的响应类型,mime类型,多个值逗号分隔 | 
| consumes | 
入参类型值,mime类型,多个值逗号分隔 | 
| notes | 
接口的详细描述 | 
| response | 
接口的响应类型。文档的响应信息结构可以根据方法的返回值自动处理。 | 
| protocols | 
支持的传输协议,如http。多个协议逗号分隔 | 
| hidden | 
是否在文档中展示改接口 | 
| responseHeaders | 
http响应头信息描述 | 
| authorizations | 
获取此操作的授权列表 | 
| code | 
HTTP返回状态码 | 
| nickname | 
第三方工具使用operationId来唯一表示此操作 | 
| tags | 
接口所属标签,默认为controller标签 | 
| extensions | 
扩展属性描述 | 
- @ApiResponses:响应信息集合,包含多个ApiResponse注解
 
- @ApiResponse:定义不同http响应码的相关相关响应信息
 
| 属性名 | 
说明 | 
| code | 
http响应码 | 
| message | 
响应码描述 | 
| response | 
响应实体类 | 
| examples | 
响应例子 | 
examples	响应例子
 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
  | 
public class OrderDetailRequest {
    @ApiModelProperty(value = "平台编码", required = true, example = "70")
    private String platformCode;
    @ApiModelProperty(value = "平台订单号", required = true, example = "sn20201029")
    private String platformOrderSn;
    @ApiModelProperty(value = "订单状态", example = "0", allowableValues = "0,1,2,3,4")
    private Integer status;
    @ApiModelProperty(value = "特殊字段", example = "0", hidden = true)
    private BigDecimal special;
    @ApiModelProperty(value = "开始时间 格式:yyyy-MM-dd HH:mm:ss")
    private String startTime;
}
@ApiModel(value = "response", description = "用于通用响应")
public class EgResponse {
    @ApiModelProperty(value = "响应码", example = "200")
    private String code;
    @ApiModelProperty(value = "响应码描述", example = "成功")
    private String message;
    @ApiModelProperty("响应数据")
    private T data;
}
  | 
 
- @ApiModel:实体的描述,主要用于响应实体,不是必须的
 
| 属性名 | 
说明 | 
| value | 
实体名称 | 
| description | 
实体说明 | 
| parent | 
设置父类型 | 
| subTypes | 
设置子类型 | 
- @ApiModelProperty:实体中字段的描述
 
| 属性名 | 
说明 | 
| value | 
字段描述 | 
| name | 
字段名称,会覆盖默认名称 | 
| allowableValues | 
允许的取值范围,主要用于请求实体中 | 
| description | 
实体说明 | 
| example | 
字段样例值。对于请求实体,该值在测试时自动填充入请求参数中;对于响应实体,则会展示在响应例子中 | 
| dataType | 
字段类型 | 
| required | 
是否必填 | 
| position | 
字段的顺序 | 
| hidden | 
是否在文档中隐藏该字段 | 
| allowEmptyValue | 
是否允许传递空置 | 
4.对应关系



表示接口过时了 Warning: Deprecated
在该接口的方法上添加@Deprecated
@Deprecated简单来讲就是,若某类或某方法加上该注解之后,表示此方法或类不再建议使用,调用时也会出现删除线,但并不代表不能用,只是说,不推荐使用,因为还有更好的方法可以调用。
在swagger中的使用:
1
2
3
4
5
6
7
8
  | 
    @ApiOperation(value = "通过站点id查询栏目列表")
    @Deprecated
    @RequestMapping(value = "findByTerminalId", method = RequestMethod.POST)
    public AjaxResponse<SelectOutputBean<AppNavigationInternalOutputBean>> findByTerminalId(
            @RequestBody @Validated @ApiParam(required = true) ConditionOfSiteModel model) {
        SelectOutputBean<AppNavigationInternalOutputBean> result = getService().findByTerminalId(model);
        return AjaxResponse.success(result);
   }
  | 
 
效果:
