springboot集成swagger

概述 

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>

简单写个controller

@RestController
public class TestSwagger {
    @RequestMapping(value = "/hello")
    public String hello(){
        return "请求成功!";
    }
}

测试:

 

集成swagger

将依赖放入pom.xml的dependencies中

//Configuration  表明是个配置类
//EnableSwagger2 开启Swagger2
@Configuration
@EnableSwagger2
public class SwaggerConfig {

}

测试地址:http://localhost:8080/swagger-ui.html

 

 

 配置swagger

Swagger的bean实例 Docket

//Configuration  表明是个配置类
//EnableSwagger2 开启Swagger2
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    //配置了swagger Docket的Bean实例
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }
    //配置swagger apiInfo类
    public ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("gj", "http://baidu.com", "20221219");
        return new ApiInfo(
                "gj-swagger",
                "swagger-hello",
                "1.0",
                "https://blog.csdn.net/weixin_43522117?type=blog",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }
}

测试:

 配置扫描接口及开关

@Bean
public Docket docket() {
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .enable(false) //是否开启与关闭swagger
            //RequestHandlerSelectors 配置要扫描接口的方式
            //basePackage: 指定要的包//any(): 扫全部
            // none():不扫趟
            //withclassAnnotation: 扫播类上的注解
            .select().apis(RequestHandlerSelectors.basePackage("com.working.study.controller"))
            // PathSelectors 过滤路径
            .paths(PathSelectors.ant("/study/**"))
            .build();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值