栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot 集成Swagger2 好用的api文档生成工具

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot 集成Swagger2 好用的api文档生成工具

目录

一、引入依赖

二、创建swagger2的配置文件

三、启动服务器

 四、对api里展现的类也可进行配置


一、引入依赖

注意:2.9.2版本与springboot2.6不兼容,需要springboot2.4.x版本或2.5.x版本

        
        
            io.springfox
            springfox-swagger2
            2.9.2
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
        
        
            com.github.xiaoymin
            swagger-bootstrap-ui
            1.6
        

二、创建swagger2的配置文件
package com.wxl.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.service.Contact;
import springfox.documentation.spi.documentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {
    //访问方法: http://localhost:8080/swagger-ui.html来访问api文档 原路径
    //访问方法: http://localhost:8080/doc.html来访问api文档 第三个依赖的路径

    //配置swagger2核心配置 docket
    @Bean
    public Docket createRestApi() {
        return new Docket(documentationType.SWAGGER_2)//指定api类型为swagger2
                .apiInfo(apiInfo())                         //用于定义api文档汇总信息
                .select()
                .apis(RequestHandlerSelectors
                        .basePackage("com.wxl.controller"))  //指定controller包
                .paths(PathSelectors.any())   //所有controller
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxx 平台接口api")   //文档页标题
                .contact(new Contact("平台名字",
                        "http://www.xxx.com",
                        "123456@xx.com"))   //联系人信息
                .description("专为xxx提供的api文档")  //详细信息
                .version("1.0.1")   //文档版本号
                .termsOfServiceUrl("http://www.xxx.xxx.com")   //网站地址
                .build();
    }

}

三、启动服务器

访问如下:

http://localhost:8080/swagger-ui.html

如果添加了我的第三个依赖还可以访问如下连接:

http://localhost:8080/doc.html#/ 

 四、对api里展现的类也可进行配置

如:添加了@ApiIgnore注解以后,该文档就不会生成该接口的对应项了

也可以在相应的类上添加如下:

@Api会在生成的文档里对该项进行相应的描述,方便查阅。

 也可以在方法上添加api介绍:

@ApiOperation生成的api会有相应的方法介绍

也可以在使用到的对象上进行api解释:

@ApiModel 用于解释api的实体对象 

还可以对实体类对应的属性上进行api解释:

@ApiModelProperty 解释每个参数的含义 

最终效果:

可以看到解释以后直观多了。 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/785259.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号