Spring boot 集成Swagger

简介

Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。本文简单介绍了在项目中集成swagger的方法。
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。

环境

  • Spring boot 2.0.5
  • Swagger springfox-swagger-ui 2.6.1
  • springfox-swagger2 2.6.1

步骤

新建Spring boot项目
添加Swagger依赖
<!--swagger-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>
添加Swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.imjcker.springswagger.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档注释")
                .description("接口文档注释说明")
                .termsOfServiceUrl("帮助连接")
                .contact(new Contact("imjcker", "http://imjcker.com", "helloalanturing@icloud.com"))
                .version("1.0")
                .build();
    }
}

测试

@RestController
@Api(description = "测试接口")
public class TestController {

    @ApiOperation("测试方法")
    @GetMapping("/doTest")
    public String test(String param1) {
        System.out.println("param1 = [" + param1 + "]");
        return "TestController.test";
    }
}

结果展示

启动项目,浏览器访问:http://localhost:8080/swagger-ui.html
在这里插入图片描述

总结

更详尽的配置,请参考官方网站!

附件下载

教程源码
https://download.csdn.net/download/u012137018/10698329

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天府书虫

来杯咖啡☕️~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值