Swagger介绍以及使用方法

Swagger

是什么

一个api工具 

为什么

为了前后端分离方便前后端统一接口

怎么用

###1.导入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>

###2配置.SwaggerConfig

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api(Environment environment){
        Profiles profiles = Profiles.of("dev", "test");//找到此是swagger所处的环境  开发dev  测试test  发布
        boolean flag = environment.acceptsProfiles(profiles);//判断是否处在设定的环境
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag)//默认打开true  是否启用swagger
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.joker.controller"))//扫描  后边的方法是基于什么扫描
                .paths(Predicates.or(PathSelectors.ant("/Calculate/export"),
                        PathSelectors.ant("/Calculate/insert/*/*")))//过滤什么路径  全部any  全部不none   ant路径
                .build()
                .useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.GET, newArrayList(
                        new ResponseMessageBuilder()
                                .code(500)
                                .message("服务器发生异常")
                                .responseModel(new ModelRef("Error"))
                                .build(),
                        new ResponseMessageBuilder()
                                .code(403)
                                .message("资源不可用")
                                .build()
                ));

    }
    private ApiInfo apiInfo() {
        return new ApiInfo(
                //大部分是不必写的
                "使用Knife4j增强版的swagger作业",  //标题
                "",  //描述
                "",  //版本
                "",//跳转链接
                new Contact("名字想好没", "", ""),
                "", "", Collections.emptyList());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值