美文网首页
spring-boot-swagger

spring-boot-swagger

作者: jianshuqiang | 来源:发表于2019-05-30 16:57 被阅读0次

第一步:引入GAV

 <!--Swagger-->
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <!--  guava 开源的java库      -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>27.1-jre</version>
        </dependency>

第二步编写配置文档

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //选择controller包
                .apis(RequestHandlerSelectors.basePackage("com.sgcc.dls.imnotice.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //自定义信息可按需求填写
                .title("即时通讯与通知服务-API文档")
                .description("接口文档")
                .termsOfServiceUrl("http://")
                //contact 作者信息
                .contact(new Contact("张强", "", ""))
                .version("1.0")
                .build();
    }

}

以上配置文件主要生成页面中的一些信息,例如接口扫描的包文件,文档的标题描述等信
第三步 使用注解
Swagger官方注解
https://github.com/swagger-api/swagger-core/wiki/Annotations

名称 描述
@Api 将类标记为Swagger资源。
@ApiImplicitParam 表示API操作中的单个参数。
@ApiImplicitParams 一个包装器,允许列出多个ApiImplicitParam对象。
@ApiModel 提供有关Swagger模型的其他信息。
@ApiModelProperty 添加和操作模型属性的数据。
@ApiOperation 描述针对特定路径的操作或通常是HTTP方法。
@ApiParam 为操作参数添加其他元数据。
@ApiResponse 描述操作的可能响应。
@ApiResponses 一个包装器,允许列出多个ApiResponse对象。
@Authorization 声明要在资源或操作上使用的授权方案。
@AuthorizationScope 描述OAuth2授权范围。
@ResponseHeader 表示可以作为响应的一部分提供的标头。

第四步 进行访问
访问地址
ip:端口号/swagger-ui.html

@Api(value="认证控制器", tags={"认证接口"})

tags将显示在swagger的页面中


image.png

相关文章

  • spring-boot-swagger

    第一步:引入GAV 第二步编写配置文档 以上配置文件主要生成页面中的一些信息,例如接口扫描的包文件,文档的标题描述...

网友评论

      本文标题:spring-boot-swagger

      本文链接:https://www.haomeiwen.com/subject/kppvtctx.html