Swagger2为程序生成文档

2023-08-10 12:02:00 浏览数 (1)

前言:

swagger2就相当于一个实时同步文档,给前端提供接口的插件。将平常写注释前面加个注解就可以了,非常的方便,但是生产环境为了安全和运行效率,需要关闭(抄自知乎)

1.引入依赖

可在全局引入,如:api

代码语言:javascript复制
 <!-- swagger -->
        <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.添加配置类

在config包下,新加Swagger2

代码语言:javascript复制
package com.zb.config;

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;

/**
 * @author zh
 * @ClassName cn.saytime.Swgger2
 * @Description
 * @date 2017-07-10 22:12:31
 */
@Configuration
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zb.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot利用swagger构建api文档")
                .description("简单优雅的restfun风格")
                .termsOfServiceUrl("#")
                .version("1.0")
                .build();
    }
}

3.控制器使用注解

ApiOperation的,代表该方法名 (value=名字1,notes=名字2)

ApiImplicitParam的,代表参数名 (name=参数名,value=参数介绍,paramType=接受参数方式,dataType=参数类型)

参照图&代码:

代码语言:javascript复制
 @ApiOperation(value = "商品详情", notes = "商品详情-notes")
 @ApiImplicitParam(name = "skuId", value = "商品编号", required = true, paramType = "path", dataType = "string")

paramType 对照表:

paramType = “path”

paramType = “query”

paramType = “body”

paramType = “header”

@PathVariable(“skuId”)

@RequestParam(“skuId”)

@RequestBody

@RequestHeader

@GetMapping(“/info/{skuId}”)

无需加

无需加

无需加

多个参数:

代码语言:javascript复制
 @ApiOperation(value = "修改商品", notes = "修改商品-notes")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuId", value = "商品编号", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "skuDto", value = "商品对象", required = true, paramType = "body", dataType = "SkuDto")
    })

4.实体类使用注解

如果有dto,内部的实体类就不用注解

参数介绍: ApiModel: (value=类名字,description=类介绍)

ApiModelProperty: (value=参数介绍)

代码语言:javascript复制
@ApiModel(value = "OrderItemDto", description = "")
@ApiModelProperty(value = "这个是ID")

5.启动

启动类添加

代码语言:javascript复制
@EnableSwagger2

程序启动完后,浏览器打开:http://localhost:端口号/swagger-ui.html

0 人点赞