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

Swagger接口测试(项目搭建)

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

Swagger接口测试(项目搭建)

接上篇文章Swagger接口测试

官网了解一下Swagger

https://swagger.io/

现在开始配置Swagger了

一. 添加两个依赖


            
                io.springfox
                springfox-swagger2
                ${swagger.version}
            
            
            
                io.springfox
                springfox-swagger-ui
                ${swagger.version}
            

注意一下版本

 二. 写一个配置类

package config;

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.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 Swagger2Config {

    @Bean
    public Docket webApiConfig(){

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                //只显示api路径下的页面
                .paths(Predicates.and(PathSelectors.regex("/api/.*")))
                .build();

    }

    @Bean
    public Docket adminApiConfig(){

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                //只显示admin路径下的页面
                .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
                .build();

    }

    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("网站-API文档")
                .description("本文档描述了网站微服务接口定义")
                .version("1.0")
                .contact(new Contact("buba", "http://www.baidu.com", "3332222@qq.com"))
                .build();
    }

    private ApiInfo adminApiInfo(){

        return new ApiInfoBuilder()
                .title("后台管理系统-API文档")
                .description("本文档描述了后台管理系统微服务接口定义")
                .version("1.0")
                .contact(new Contact("buba", "http://www.baidu.com", "3332222@qq.com"))
                .build();
    }


}

 小笔记:regex----正则表达式的意思

三. 刷新maven 

 了解一下注解(不用操作!!!)

启动测试类

到网页上运行http://localhost:8201/swagger-ui.html#/

 

这是了解测试接口的注解(有兴趣可以了解一下)网址:

https://blog.csdn.net/xiaojin21cen/article/details/78654652

小错误:

结局方案:在启动类上加个注解 或者再刷新一下maven

@EnableSwagger2//解决配置swagger弹窗问题

 “swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。”

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

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

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